0

I've written a plugin that connects to another service to then display its data within my EE site.

Certain parameters are necessary for each tag - primarily things like a key, username. Is there a way to declare these once rather than within each function's parameter array? I'm currently doing that, which works, but seems… like a lot of repetition.

2 Answers 2

1

Just set them as class variables inside your plugin's __construct function.

function __construct()
{
    $this->my_api_key = 'foo';
    // OR
    $this->my_api_key = ee()->TMPL->fetch_param('my_api_key');
}

Then inside your other functions, just look at $this->my_api_key.

UPDATE

You can also do a hybrid of my and Yuri's answer:

function __construct()
{
    if($my_api_key = ee()->config->item('my_addon_my_api_key'))
    {
        $this->my_api_key = $my_api_key;
    }

    if($my_api_key = ee()->TMPL->fetch_param('my_api_key'))
    {
        $this->my_api_key = $my_api_key;
    }
}
3
  • Thanks Derek. Is one solution considered a better practice than the other? I was going to move them to config vars to save time/code within the template tag. Oct 9, 2014 at 21:33
  • Generally I don't like to require that add-on users add config values in oder for my add-on to work, though it's good to offer as an option. I'll edit my answer to include a hybrid approach. Oct 10, 2014 at 15:51
  • Nice, thanks! Annnnd…more letters here so SE will let me say that. Oct 10, 2014 at 16:14
1

I would recommend moving the configuration values to EE config (set them using EE config editor). They will be available as global config values then and you can access them like this: $this->EE->config->item('my_plugin_username)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.