Jump to content

Looking for a way to modify front variables from module without override


misthero

Recommended Posts

I'm looking to a way to modify the output of some prestashop pages from a module without using an override file and class and I need help

 

we can now assign some variable to every tpl file just with:

$this->context->smarty->assign('foo',$bar);

now in every template file {$foo} is available and will output $bar value. But that is only true if $foo is not declared somwhere else.

 

for example in category page you have the $category object but i cannot find any smart way to manipulate it, for example if you want remove the category image you could simply unset the public variable $id_image, and the obvious way would be

$myCategoryOutput = new Category($categoryID,$this->context->language->id);
$myCategoryOutput->id_image = '';
//or
unset($myCategoryOutput->id_image);
//and then send back the modified object to the template
$this->context->smarty->assign('category',$myCategoryOutput);

this simply doesn't work, it is ignored or overwrited later in the process of the page rendering.

 

any idea??

 

 

Link to comment
Share on other sites

Did you ensure that your module is the last hook to execute (Module -> positions)? The problem is likely to do with the order of execution and it will depend greatly on where the variable is assigned (controller or module) and where the hook that you're using in your module is executed with respect to the default assignment.

 

IIRC you can also use the following to remove the assigned variable altogether:

$smarty->clear_assign('category');
Edited by Paul C (see edit history)
  • Like 1
Link to comment
Share on other sites

thank you for your answer, maybe you pointed me in the right direction

 

for smarty 3 it the method is now named clearAssign() so it should be

$this->context->smarty->clearAssign('category');

the object $category in my example is set by CategoryController, I think the order of execution is exactly the problem, but i have yet to find when exactly I should call the method, and possibly it should be indipendent from module position.

 

I'm going to test a little.

Edited by misthero (see edit history)
Link to comment
Share on other sites

ok this is what I discovered:

 

if i place this code:

if (Tools::getValue('controller') == 'category') {
            $this->context->smarty->clearAssign('category');
            $category =  new Category(Tools::getValue('id_category'), $this->context->language->id);
            $category->id_image = 0;
            $this->context->smarty->assign('category', $category);
        }

inside hookDisplayFooter if works and successfully replace 'category' variable

 

but it doesn't work in other hooks like hookDisplayTop regardless of the module position in any given hook

 

is there a particular order for hooks being called?

Link to comment
Share on other sites

after more testing I got the order followed by standard and custom hooks, related to the category controller during the render process, i don't know why, by it looks like the displayFoot is the only one being called after the controller and before the actual rendering of the page starts so it is the only place where you can change controllers variables assigned to smarty, anyway i didn't tested everything so I'm unsure if there is anything else in the middle

 

  1. header
  2. top
  3. left
  4. right
  5. categoryController
  6. foot

any custom hook like {hook h="myHook"} is being called during the render process only if found in template file (logically) but at that time is too late to change anything because the variables have already been used and changing it will have no effect on what is displayed.

 

i was expecting a different behaviour really and I would like to insert modification to controller variables without using any hook, but i don't know how or if there is any function other than hookDisplayFooter to do that.

 

for now I will adapt to this findings and will use the footer hook, but if anyone got any other way I would be happy to know.

 

thanks!

Link to comment
Share on other sites

  • 4 weeks later...

I would really like to discuss this with other developers, I feel like I'm talking alone, anyway I will share what I found.

 

clearAssign is not needed at all, I discovered there is way to overload a smarty variable this way:

$variable = $this->context->smarty->getVariable('variable_name');

now any manipulation you make to "$variable" will be reflected in smarty "variable_name", no need to assign again to smarty again. Pretty awesome.

 

anyway this only work for smarty variables assigned to the context and available to every tpl.

 

If anyone is willing to join the discussion I still have 2 questions unanswered:

 

1) is there a better place/hook something where to injiect those manipulation than "hookDisplayFooter" ?

2) anyone know how to manipulate smarty variables assigned to a specific tpl only and not available in other?

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