Target Specific Pages from a PrestaShop Module’s Hook

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:

$this->context->controller->php_self

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:


	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'));
	}

And add this at the beginning of the function:

if('index' != $this->context->controller->php_self)
	return false;

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:

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

Looking for quality PrestaShop Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under PrestaShop | RSS 2.0

Author Spotlight

Fabio Porta

Fabio Porta

Fabio has been involved in web development and design since 2005, when launched his first website at the age of 16. He’s now highly skilled in both client and server side development, along with design, and since August 2012 runs a successful website about PrestaShop tutorials and Prestashop Modules called Nemo’s Post Scriptum, at http://nemops.com

Leave a Reply

Your email address will not be published. Required fields are marked *


2 − 1 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>