Jump to content

[SOLVED] How to add new HOOK in prestashop 1.4


Recommended Posts

ok, our new hook named is HOOK_CENTER

I attach the code in header.tpl

                 
{$HOOK_CENTER}                 


and attach in classes/FrontController.php in line:394

'HOOK_CENTER' => Module::hookExec('center'),



the problem is: How to appear a new HOOK in Back Office > Modules Positions page?
that appears also to be used for transplant module column.

sorry for my bad English ;-P
thanks

37943_VjteX1IS6Lt9LXsjvWw1_t

Link to comment
Share on other sites

You should also NOT be modifying the classes/FrontController.php file at all, but providing an override as `override/classes/FrontController.php`

The override file could be as simple as (untested):

<?php
class FrontController extends FrontControllerCore
{

 function displayHeader()
 {
   parent::displayHeader();

   $this->smarty->assign(array('HOOK_CENTER' => Module::hookExec('center')));
 }
}



As mentioned you also need to add an entry in the database table, so you can actually use it.

Paul

  • Like 3
Link to comment
Share on other sites

ok.. maybe i'm missing something, but i can't get my new hook to work.

I have

1. added a new Hook in my DB called "lala"
2. made a new file in override/classes/ called Frontcontroller.php
3. added

<?php
class FrontController extends FrontControllerCore
{
 function displayHeader(){

           parent::displayHeader();
           $this->smarty->assign(array('HOOK_LALA' => Module::hookExec('lala')));
           }
} 

?>



to FrontController.php
4. added {$HOOK_LALA} to my header.tpl

BUT... nothing is happening. I got the hook displayed in the back end but when i add modules they aren't displayed on my page...

Any suggestions? I'm pretty clueless right now. I really like PrestaShop but the Hook-System sucks. It's a core function in templating that should be MUCH simpler to implement.

Link to comment
Share on other sites

I think this could be down to a slight misunderstanding as to how hooks work; as to actually display anything the module you're hooking in must support the hook and I'm not sure you have done that?

In your module install() function you would execute (OR you can do nothing in the module and rely on the Backoffice "transplant" functionality which does exactly the same thing):

$this->registerHook('lala');



But in your module class you would always need to implement:

public function hookLala($params)
{
   echo 'la,la,la,la,la';
}



If you had used:

$this->smarty->assign(array('HOOK_LALA' => Module::hookExec('leftColumn')));



Then you could transplant any module that "supports" rendering to the left column to the hook "lala". The support in the module relies on a public member function that is named "hook", which in this case is "hookLala" (by convention the first letter of your hook name is made upper case - no idea why.... "hooklala" may in fact work just the same and the case may be ignored - haven't ever tried I don't think).

Paul

Link to comment
Share on other sites

Hi Paul!

Thank you for your answer.

as to actually display anything the module you’re hooking in must support the hook and I’m not sure you have done that?


I figured that out myself at 04:00 this morning after roughly 5 hours of searching for an answer.

And yes, you are right, i didn't put the proper function in the modules i wanted to display at my hook (btw: it's not case sensitive, hookLulu and hooklulu both work).

Prestashop is pretty neat, but as stated before - the hook system needs some serious improvement IMO. To display the category-block in a custom area you need to modify

1. the header.tpl
2. the header.php
3. the FrontController.php AND
4. the blockcategories.php

... that's a rediculous amount of work just to display a module! Compared to other CMS (yes i know, it's not a CMS, it's a shopsystem) like Expression Engine, where you can put rendering-commands like {exp:comment:entries} anywhere you want or Wordpress, where you just drag and drop your widget to the proper Sidebar, Prestashop needs improvement.
Link to comment
Share on other sites

@ometiclan I've just published an article on my site you find interesting regarding using theme plugins as an alternative to hooks for module output in Prestashop 1.4.

It seems to work for me in the case of adding the home featured products output at the bottom of cms pages, and saves a lot of work and messing about with custom hooks and database changes.

Paul

Link to comment
Share on other sites

  • 2 weeks later...
I think this could be down to a slight misunderstanding as to how hooks work; as to actually display anything the module you're hooking in must support the hook and I'm not sure you have done that?

In your module install() function you would execute (OR you can do nothing in the module and rely on the Backoffice "transplant" functionality which does exactly the same thing):
$this->registerHook('lala');



But in your module class you would always need to implement:

public function hookLala($params)
{
   echo 'la,la,la,la,la';
}



If you had used:

$this->smarty->assign(array('HOOK_LALA' => Module::hookExec('leftColumn')));



Then you could transplant any module that "supports" rendering to the left column to the hook "lala". The support in the module relies on a public member function that is named "hook", which in this case is "hookLala" (by convention the first letter of your hook name is made upper case - no idea why.... "hooklala" may in fact work just the same and the case may be ignored - haven't ever tried I don't think).

Paul



Paul you're a helpful poster but I've noticed that neither on your blog or your forum - you never post what file or line the code in reference is supposed to be. What gives
Link to comment
Share on other sites

Well in most cases that's because the discussion is about general development issues. In this thread, for example, It's impossible to specify what the filename or the linenumber to modify in ometical's example would be as he doesn't specify which module(s) he would like to hook, and we're talking about adding a new class member function (the choice of where it's added would be up to the person making the change - it could be anywhere in the class definition).

In all honesty I'm not likely to tell anyone which Prestashop core files to hack to get something to work, as that is just setting them up to spend hours asking questions on here later when they next try and upgrade and find that they can't. I have always taken the approach that none of the core files should ever be modified to produce custom behaviour; and if you're going to modify them, then you should know what you're doing and not have to ask or you'll be forever dependent upon others goodwill to bail you out.

Another issue is that for many of the core supplied modules the version numbers haven't been updated even though the actual code in the modules has changed (through bug fixes - some of which have been major). This means that unless you have exactly the same "version 1.1" of the module that's under discussion, then the line numbers (and some of the specific code quoted) will be meaningless.

In saying the above, if there's ever a case where you feel that any of the information I supply is insufficient, then you can always ask for clarification and I may or may not notice and reply ;)

Paul

Link to comment
Share on other sites

That's cool dude. It would be helpful though if you gave some indicator of what you're talking about though
i.e. "right after the function
hookRightColumn($params){


}
you know cause people are equally willing to try and guess what line of code you're referring to and mess it up all the same

Link to comment
Share on other sites

  • 4 weeks later...

Hi Paul,

When I assign a new hook to header.tpl in the FrontControllerCore class it works.
But when i try to override it in (override/classes/FrontController.php) the new hook isn't assigned :

class FrontController extends FrontControllerCore
{

public function displayHeader()
 {
   parent::displayHeader();
   $this->smarty->assign(array('HOOK_NAV_BAR' => Module::hookExec('navBar')));
 }
}



The overriding class is taken in account. I tried to copy entire core class to overriding class and assign new hook and it works as well.

But as it's not clean to override all class since I need just a part of a function to be overrided, i'm looking for a solution.

Thx for you answers.

Link to comment
Share on other sites

You have to call the displayHeader function after assigning the new hook.

Hi Paul,

When I assign a new hook to header.tpl in the FrontControllerCore class it works.
But when i try to override it in (override/classes/FrontController.php) the new hook isn't assigned :

class FrontController extends FrontControllerCore
{

public function displayHeader()
 {
   parent::displayHeader();
   $this->smarty->assign(array('HOOK_NAV_BAR' => Module::hookExec('navBar')));
 }
}



The overriding class is taken in account. I tried to copy entire core class to overriding class and assign new hook and it works as well.

But as it's not clean to override all class since I need just a part of a function to be overrided, i'm looking for a solution.

Thx for you answers.

  • Like 1
Link to comment
Share on other sites

When i make that override the center column and right column dissaper.

I`ve adde to DB ps_hook: id 67, slider, slider (desc)

I`ve added to header.tpl

                              .....
               <!-- Left -->

                   {$HOOK_LEFT_COLUMN}

              <- here
                   {$HOOK_SLIDER}     <- & here
                     <- & here
               <!-- Center -->

               {
   {/if}
  • Like 9
Link to comment
Share on other sites

Well it seems that you don't override anything since you modfied FrontController.php file.
Sending my files won't help you.

Is there any errors displayed ?
If not are you sure you enabled the display of errors?

@ini_set('display_errors', 'on');

Link to comment
Share on other sites

@ometiclan I've just published an article on my site you find interesting regarding using theme plugins as an alternative to hooks for module output in Prestashop 1.4.

It seems to work for me in the case of adding the home featured products output at the bottom of cms pages, and saves a lot of work and messing about with custom hooks and database changes.

Paul


Paul C, that article you published just saved my life !!!

I was struggling with hooks for showing the "JQuery Nivo Slider" inside the "header.tpl" of my template, but used you code files and added the line:

{plugin module='slideric' hook='home'}



Inside "header.tpl", and it just worked perfectly!!!

You can see the result here: http://www.elpalaciodelsexo.com/ (site is still work in progress)

Regards! :)

Link to comment
Share on other sites

Hi!
I use the Paul's solution and it's works fine, but I'm trying to make the same thing that xacoso but in his shop http://www.elpalaciodelsexo.com/ when you go away from the homepage (category, product page, etc) the slider does not appear. In my shop the slider is always is showed.

I'm using the same code in header.tpl

                   {$HOOK_TOP}



                   {plugin module='slideric' hook='home'}


               <!-- Left -->

                   {$HOOK_LEFT_COLUMN}



There is some more code that it should modify?
Maybe a specific configuration in "Positions" in backoffice?

Link to comment
Share on other sites

There should be a smarty variable {$page_name} defined - it's used in the <body> tag in header.tpl to use as the id for the body of each page. I think you could test that to see if you are on the index page e.g.


                   {$HOOK_TOP}


               {if isset($page_name) AND $page_name=='index'}

                   {plugin module='slideric' hook='home'}

               {/if}

               <!-- Left -->

                   {$HOOK_LEFT_COLUMN}
 

Link to comment
Share on other sites

A little tip: In this situation div can't be id="slider" because it enters conflict with the class of the module and the module don't works fine.
This is my final code:

{if isset($page_name) AND $page_name=='index'}

                   {plugin module='slideric' hook='home'}

             {/if}

Link to comment
Share on other sites

You have to call the displayHeader function after assigning the new hook.



I think I'm being a bit thick here! I have the same problem as smashit had, but I'm wondering where I have to call the displayHeader function? And if so, how? lol
Link to comment
Share on other sites

  • 2 weeks later...
Hi Paul!
Prestashop is pretty neat, but as stated before - the hook system needs some serious improvement IMO. To display the category-block in a custom area you need to modify

1. the header.tpl
2. the header.php
3. the FrontController.php AND
4. the blockcategories.php


How do we make this entire process upgrade safe? I know if we add overrides/classes/FrontController.php that file will be fine and if we put header.tpl into the themes/mytheme/ it will be safe too. However, so far I can't tell that there is a safe way to modify 2 and 4 in an upgrade safe manner.

By the way good thread. I've been through the boards quite thoroughly (at least the english ones) and this is the best one on adding a new Hook. [applause]

Wil
Link to comment
Share on other sites

  • 3 weeks later...
Well it seems that you don't override anything since you modfied FrontController.php file.
Sending my files won't help you.

Is there any errors displayed ?
If not are you sure you enabled the display of errors?

@ini_set('display_errors', 'on');


Since nobody has offered a working override example i started another thread to find out how to use the override code correctly. See here :
http://www.prestashop.com/forums/viewthread/109195
Link to comment
Share on other sites

  • 2 months later...
  • 5 months later...

Hey guys

 

I'm having the same problem with the block search top...i`m trying to move it to a new hook that i created....its called 'headerSearch' ....i attached it to header.tpl....i added the new hook in db.. and i modified the blocksearch.php by adding the new hook in the source

line 53  OR !$this->registerHook('headerSearch')

 

line 87-91
public function hookheaderSearch($params)
{
 $this->_hookCommon($params);
 return $this->display(__FILE__, 'blocksearch-top.tpl');
}

 

aftere that i transplanted it to the new hook....but the module it doesnt appear in the new hook

what should I do?!

Link to comment
Share on other sites

  • 4 months later...
  • 2 years later...

You should also NOT be modifying the classes/FrontController.php file at all, but providing an override as `override/classes/FrontController.php`

 

The override file could be as simple as (untested):

 

<?phpclass FrontController extends FrontControllerCore{  function displayHeader()  {    parent::displayHeader();    $this->smarty->assign(array('HOOK_CENTER' => Module::hookExec('center')));  [spam-filter]

As mentioned you also need to add an entry in the database table, so you can actually use it.

 

Paul

 

Hello dear Paul, I am trying in adding a new hook i my prestashop6 ,so I am doing the following steps:

1. The name of my hook is bottom

2. I have added the hook in my ps_table

name: displayBottom title: displayBottom

 

3.created the FrontController.php like this:

<?php class FrontController extends FrontControllerCore{  function displayBottom()  {    parent::displayBottom();    $this->smarty->assign(array('HOOK_BOTTOM' => Module::hookExec('bottom')));  [spam-filter]

 

4. I uploaded in the following directory

override/classes/controller

 

Then I go in the Theme configurator in my presta shop and the only hooks I see in the page are the top,left, right and footer.

Will it be possible to let me know what I am doing wrong.

Thanks in advance

Marina

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...