Jump to content

(solved) returning array trough hook


Richard S

Recommended Posts

Hello,

 

It only now come to my mind a question - can you return an array trough hook?

 

So simple hook:

 

  public function hookMyHook($params)
{
return array('test');
}

 

But with print_r() function I only get Array. In addition, it does not show Array () or something like that, so I am confused is this a restriction or something is weird at me, because hook is definitely called and executes. It also returns strings, numbers etc., but what about an array? Have you ever had this problem?

  • Like 1
Link to comment
Share on other sites

I have overridden OrderController function called public function preProcess().

 

Everything is the same except add two lines in after minimalPurchase if section and I have added only this:

 

		$resultArray = Module::hookExec('myHook', array('cart' => self::$cart, 'step'=>$this->step));
	print_r($resultArray);

 

I would like to return step and error arrays to use them later in preProcess function. And yes, maybe doing this as separate function in Controller would be easier, but I want to maintain all logic in module class and only call to it if it is necessary.

 

And here is my hook function:

 

public function hookMyHook($params)
{
	return array('test1', 'test2');
}

 

If I write instead of arary(..) string or number everything is ok and print_r() is showing them, but if I am doing as it is here, i could only see "Array" and that's strange because it should write all Array elements or write "Array ()", if it is empty. I think this is a problem in my side, but cannot figure what's wrong. It should be something obvious which I cannot see :)

 

EDIT: By the way hook is created in module, so it is not a PrestaShop default hook

Link to comment
Share on other sites

Ok I see the problem : you can't return an array or an object from hooks, because there may be several plugins hooked to your hook. So what should Module::hookExec() return if you have 2 plugins using this hook ;) ? So you can only return a string; which will be concatened to other plugins results.

 

Anyway you can do something for your problem : getting the array by reference.

 

$results = array();
Module::hookExec('myHook', array(
  'cart' => self::$cart,
  'step'=> $this->step
  'results' => &$results,
));
print_r($results);

       public function hookMyHook($params)
       {
               $params['results'] = array('test1', 'test2');
       }

This should work ;)

 

Regards

Link to comment
Share on other sites

Oh.. It is really the first time in two years I tried to do this, so one of my thoughts come true that it is not allowed to return array. Yes, I have also thought about doing this by reference, but wanted to be sure about restrictions of array in returns statements.

 

In addition, you should update documentation with this as I look trough documentation on Wiki etc. did not find anything mentioned. If it was mentioned, I would not write here ;)

 

Thank you again.

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...