5

This seems like a bug and I tested on both 2.5.x and 3.x but there is no way to get assigned template name in onAfterInitialise by using

$app = JFactory::getApplication();
$templatename =  $app->getTemplate();
    echo $templatename;

What it actually does once you call $app->getTemplate() it switches your assigned template to default one.

In my system plugin I do this

public function onAfterInitialise() {

    $app = JFactory::getApplication();
    $templatename =  $app->getTemplate();
    echo $templatename;

}

It works fine if I use it in onAfterRoute but as asked I need it inside onAfterInitialise.

Can anyone please post possible solution to get assigned template name in onAfterInitialise. SQL , joomla method anything. Thank you!

UPDATE: Any method from JFactory::getApplication when called within onAfterInitialise , resets your assigned template to default one. I doubt there is any solution to this since even if we call $app->getMenu()->getActive() to see assigned template our assigned template is reset to default one.

This works only if SEF is OFF:

/**
 * Get the assigned frontend template name
 *
 * @return  string  - template name
 */
public $assignedTemplate = '';

public function getAssignedTemplate() {

    $jinput   = JFactory::getApplication()->input;
    $current  =  $jinput->getInt('Itemid');

    $db    = JFactory::getDbo();
    $query = $db->getQuery( true );

    $query
    ->select($db->quoteName(array('m.template_style_id', 't.template')))
    ->from($db->quoteName('#__menu', 'm'))
    ->join('INNER', $db->quoteName('#__template_styles', 't') . ' ON (' . $db->quoteName('m.template_style_id') . ' = ' . $db->quoteName('t.id') . ')')
    ->where($db->quoteName('m.id') . ' = '.$current.'');


    // Make sure there aren't any errors
    try{

        $db->setQuery($query);
        $currentTemplate = $db->loadObjectList();
        if($currentTemplate){
            $this->assignedTemplate = $currentTemplate[0]->template;
        }
        //

    }catch (RuntimeException $e){
        echo $e->getMessage();
        exit;
    }

    return $this->assignedTemplate;
}



public function onAfterInitialise() {

    echo $this->getAssignedTemplate();

}
  • what reason do it in onAfterInitialise? it not available in this time, so you need use DB query ... but, for any template manipulation I would suggest onBeforeRender or onAfterDispatch – Fedik Jun 9 '14 at 21:26
  • When you say 3.x, which exact version of Joomla do you mean? Cause I see what you mean about Joomla 2.5, but in 3.3 it works fine – Lodder Jun 9 '14 at 21:31
  • @Fedik I am not manipulating anything , I need the name of the assigned template that is all. And it should not switch template to default if used in onAfterInitialise – Dan Jun 9 '14 at 22:16
  • @Lodder 3.3 , it kicks me back to protostar, same as in 2.5.20 kicking back to beez. note , assign some other template to other page and go to it , it will kick it to default one. just updated to 3.3.1.rc. same thing – Dan Jun 9 '14 at 22:17
  • 1
    @Fedik I need to read assigned template xml file in onAfterInitialise and since it is kicking it to default one I dont have the assigned one info – Dan Jun 9 '14 at 22:24
7
+50

I may be wrong but I don't think it will work at all before the routing part is done. Simply because the active template depends on the routing.

If you want to do it in the onAfterInitialise event for whatever reason, you likely need to query the database table #__menu and read the template_style_id for the current Itemid.

| improve this answer | |
  • It would work if the template would not be reset to default one. Try the code above I posted and you will see what I mean. – Dan Jun 12 '14 at 20:26
  • thnx for leading me to this. Yes it works via $jinput->getCmd('Itemid'). Bounty can be awarded in 22h – Dan Jun 12 '14 at 20:51
  • 2
    $jinput->getInt('Itemid') would even be a bit better since it always is an integer. – Bakual Jun 12 '14 at 22:48
  • nice , will post the full sql – Dan Jun 13 '14 at 8:57
  • Will it work when Joomla SEF is enabled? – Gaurav Jun 14 '14 at 10:09
1

This works only if SEF is OFF

/**
 * Get the assigned frontend template name
 *
 * @return  string  - template name
 */
public $assignedTemplate = '';

public function getAssignedTemplate() {

    $jinput   = JFactory::getApplication()->input;
    $current  =  $jinput->getInt('Itemid');

    $db    = JFactory::getDbo();
    $query = $db->getQuery( true );

    $query
    ->select($db->quoteName(array('m.template_style_id', 't.template')))
    ->from($db->quoteName('#__menu', 'm'))
    ->join('INNER', $db->quoteName('#__template_styles', 't') . ' ON (' . $db->quoteName('m.template_style_id') . ' = ' . $db->quoteName('t.id') . ')')
    ->where($db->quoteName('m.id') . ' = '.$current.'');


    // Make sure there aren't any errors
    try{

        $db->setQuery($query);
        $currentTemplate = $db->loadObjectList();
        if($currentTemplate){
            $this->assignedTemplate = $currentTemplate[0]->template;
        }
        //

    }catch (RuntimeException $e){
        echo $e->getMessage();
        exit;
    }

    return $this->assignedTemplate;
}



public function onAfterInitialise() {

    echo $this->getAssignedTemplate();

 }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.