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();
}
onAfterInitialise
? it not available in this time, so you need use DB query ... but, for any template manipulation I would suggestonBeforeRender
oronAfterDispatch