Add a hook in CMS pages

10 September

Prestashop CMS pages are useful, but lack the possibility of adding modules to any of them, specifically. Let’s see how to enhance these pages and create custom hooks for them!

Creating a custom hook for CMS pages

Locate your cms.tpl inside the module’s folder, and open it up in any code editor.

We first need to decide where to put our module, if before the cms content, or after it. Whichever position you choose , the important section of the page is the following:

1
2
3
<div class="rte{if $content_only} content_only{/if}">
    {$cms->content}
</div>

I strongly advise to insert new hooks right outside the rte container block, to avoid cms styles to override the ones of any eventual module. At this point, let’s add the hook.

1
2
3
4
5
{hook h='customCMS'}
<div class="rte{if $content_only} content_only{/if}">
    {$cms->content}
</div>

As you can see, I decided to add mine right before the cms content block.

How to hook Modules to CMS pages

We have our custom hook. It’s time to decide which module we want to plug to a cms page.

Given that it cannot be done without modifying core module files, to make our lives easier it’s generally a good idea to simply clone/call an existing hooking method.

In the example, I will use the myCollectionPlaces module, to display the places list in a cms page. Therefore, open up mycollectionsplaces.php, located at modules/mycollectionsplaces, and then locate the install method:

1
2
3
4
5
public function install()
{
    global $cookie;
    return (parent::install() AND myOwnReservationsController::install($this, $cookie));
}

We called our hook customCMS, so we need to register this during the module’s install:

1
2
3
4
5
6
public function install()
{
    global $cookie;
    $this->registerHook('customCMS');
    return (parent::install() AND myOwnReservationsController::install($this, $cookie));
}

Then, we need to add the hooking method at the end of the file, before the closing bracket

1
2
3
4
public function hookcustomCMS($params)
{
    return $this->hookDisplayHome($params);
}

This way, we are simply calling the default method used to show the block in the homepage.

No comments yet.

Leave a Reply