2

I have two small arrays I want to store in my plugin settings, though I'm stuck at what the AttributeType:: should be, as well as how to retrieve the actual values. The arrays I want to store look like:

'active_features' => [
     'change_team' => false,
     'change_series' => false,
     'change_user_settings' => false,
],

In my plugin file (pluginHandlePlugin.php) I see I can do something like this:

protected function defineSettings()
{
    return array(
        'pluginSettings1'   => array(AttributeType::Mixed, 'active_features' => array('change_team' => false, 'change_series' => false, 'change_user_settings' => false)),
    );
}

And getting those values through the service file works like:

public function getPluginSettings()
{
$plugin = craft()->plugins->getPlugin('pluginHandle');
$settings = $plugin->getSettings();

return $settings;
}

Which works, and returns the object for me. However I'm unable to get to the actual key/values from the original array, I've tried:

return $settings->pluginSettings1;

But that returns NULL

So, how do I retrieve the keys/values, and also, what is the best AttributeType:: to set for this? I've looked around for AttributeType:: options and details but didn't find anything.

Any help would be GREAT! Thanks!

4

As far as accessing the value of the setting, try:

$plugin = craft()->plugins->getPlugin('pluginHandle');
$plugin->getSettings()->getAttribute('pluginSettings1');

If you need to access your settings throughout your services file you might also create a global variable.

class PluginHandleService extends BaseApplicationComponent
{
    public $settings = array();

    public function __construct()
    {
        $this->settings = $this->initSettings();
    }

    private function initSettings()
    {
        $plugin = craft()->plugins->getPlugin('pluginHandle');

        $settings = array();
        $settings['pluginSettings1'] = $plugin->getSettings()->getAttribute('pluginSettings1');

        return $settings;
    }

    // other service methods

}

Which you can then access like this:

$this->settings['pluginSettings1']

Thanks to André Elvan for help on setting up a constructor.

Update

As far as assigning a value however, I think you may have to use the 'default' property. In the past I have successfully done this with strings like so:

protected function defineSettings()
{
    return array(
        'pluginSettings1' => array(AttributeType::String, 'default' => "value"),
}

I presume you could do the same with 'Mixed', although never tried:

protected function defineSettings()
{
    return array(
        'pluginSettings1' => array(AttributeType::Mixed, 'default' => 'active_features' => array('change_team' => false, 'change_series' => false, 'change_user_settings' => false))),
}

It might make more sense to break the array into individual settings, however.

protected function defineSettings()
{
    return array(
        'change_team'          => array(AttributeType::Bool, 'default' => 'false',
        'change_series'        => array(AttributeType::Bool, 'default' => 'false',
        'change_user_settings' => array(AttributeType::Bool, 'default' => 'false',
    ),
}
  • Awesome, thanks for the help! I think there's a problem with the array structure I am using for "defineSettings()": Because $settings['change_team'] returns: PHP notice "Trying to get property of non-object", and $settings['pluginSettings1'] returns: array(1) { ["pluginSettings1"]=> NULL } – taylor Feb 25 '15 at 1:26
  • 2
    I wasn't really paying attention to how you were assigning the values. I updated the answer — hopefully this is on the right track. – Douglas McDonald Feb 25 '15 at 6:36
  • The last suggestion (breaking array into individual settings) made more sense and after trying it, I was able to get things working perfectly. I can't thank you enough for helping out with this man! – taylor Feb 25 '15 at 7:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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