In this article we will show how to assign global variables to all frontend templates using a small plugin based on the 5.2 plugin system.
Since we want to add our global variables only to frontend templates we register to the secure post dispatches of frontend and widgets.
Attention: The plugin should not do some performance sensitive tasks here, otherwise each request in the storefront will be slowed down. Furthermore the assigned variables may not be compatible with the HTTP cache.
/**
* @inheritdoc
*/
public static function getSubscribedEvents()
{
return [
'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onPostDispatch',
'Enlight_Controller_Action_PostDispatchSecure_Widgets' => 'onPostDispatch'
];
}
Our example will just add the user login status to the template.
/**
* @param \Enlight_Controller_ActionEventArgs $args
*/
public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
{
$args->getSubject()->View()->assign('sUserLoggedIn', Shopware()->Modules()->Admin()->sCheckUser());
}
The complete code of this example plugin can be found here. Just unpack it to your custom/plugins
folder inside your shopware installation and activate it in the plugin manager.