Jump to content

Global variables


Recommended Posts

Hi,

 

I am trying to define a global variable that will be used in a custom module, but the module's tpl file always displays a null value. I am guessing it is a problem with the variable scope, so I am trying to create this new global variable wherever prestashop's global variables are defined. This has taken a lot of my time to figure out, but I still haven't got anything. Does someone know how to proceed?

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

Thank you for your answer, but I am not sure to understand... The hook displayHeader should be called from a FrontController from what I understand, but whenever I define a variable in my FrontController and try to display it in the module's tpl file, I am faced with the same problem.

Link to comment
Share on other sites

21 minutes ago, AminaM said:

Thank you for your answer, but I am not sure to understand... The hook displayHeader should be called from a FrontController from what I understand, but whenever I define a variable in my FrontController and try to display it in the module's tpl file, I am faced with the same problem.

Use the install function in your module to register hook like this:

    public function install()
    {
        if (!parent::install() || !$this->registerHook('displayHeader')) {
            return false;
        }
    }

And the content of the hook will be in this function:

 public function hookdisplayHeader()
    {
       code content
    }

 

Link to comment
Share on other sites

I will try your suggestiong, thanks! However, I am wondering if I can override the module's main class instead of directly adding it to the module itself, since it was bought from a third party. I am not sure how to proceed to get the hook installed while using the override principle.

 

Basically, I have a module for additionnal registration fields. Inside this module is the main php file and a template. Everything works great when I set my smarty variable directly in the main class, but it doesn't work anymore when I override it and try to get my custom changes outside of the module. That's my big issue at the moment...and I would be really grateful if someone could give me a solution for that!

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

And apart from that solution, could there be another one? Like setting the variable in a controller/class where it would be accessible by any module? I have tried putting it in most front controllers, without success.

 

If there is no other solution, I will stick to the one you suggested, since it does what I want it to do. But I have been told to avoid modifying the module's main class and to find another solution, so if there is one I haven't thought of that you would apply in a similar situation, I would love to hear it.

 

Again, thank you so much for your help and your time!

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