You can easily restrict a Prestashop module’s display by adding exceptions in the back office positions. However, if you automate the process, there is a small trick that can be used when creating a module’s hook, which will also open a whole set of new possibilities to display page-specific content.
The php_self context variable
Each Prestashop page has its own name, setup in the appropriate controller. It’s the body id assigned to a page as the $page_name variable in smarty. However, if you tried to get the page name from within a module, you might have noticed it’s only assigned to smarty. So is there a way to target each specific page from within a module? Yes, indeed. Here is the code:
[php]
$this->context->controller->php_self
[/php]
It can be really useful if you only want to target specific pages within a module. Let’s see a practical example.
Target specific pages from a module’s hook
Say, for the only sake of this example, that we only want to display a top of pages module if the current page is the homepage. Let’s use blocksearch.
In this case, we would only need to edit blocksearch.php, locate the hookTop method:
[php]
public function hookTop($params)
{
if (!$this->isCached(‘blocksearch-top.tpl’, $this->getCacheId(‘blocksearch-top’)))
{
$this->calculHookCommon($params);
$this->smarty->assign(‘blocksearch_type’, ‘top’);
}
return $this->display(__FILE__, ‘blocksearch-top.tpl’, $this->getCacheId(‘blocksearch-top’));
}
[/php]
And add this at the beginning of the function:
[php]
if(‘index’ != $this->context->controller->php_self)
return false;
[/php]
We are simply telling it: if the current page is not the homepage, kill the module so it doesn’t display anything.
Just think at the possibilities at this point: you can display different content, and do different things from the same hook, depending on the current page.
How to? First, decide the pages that need specific behaviors. Use var_dump($this->context->controller->php_self) to check each page’s exact name. Then simply add a switch or if operator, that checks the current page name and acts properly each time, like:
[php]
if(‘index’ == $this->context->controller->php_self)
// Do something and display a template for the homepage
else if(‘category’ == $this->context->controller->php_self)
// Do something and display a template if we are in the category page
else // not in any of the 2 pages above
return false: // don’t display anything
[/php]
Looking for quality PrestaShop Hosting? Look no further than Arvixe Web Hosting!