We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Retrieving add-on settings from an accessory

Development and Programming

Seamus's avatar
Seamus
47 posts
13 years ago
Seamus's avatar Seamus

I’m working on my first add-on for EE and can’t seem to figure out how to handle settings. I’ve got an extension and an accessory setup just fine. Settings in the extension get saved to the database and everything loads from the accessory as expected. But once I start trying to do stuff with the settings in the accessory I get lost.

What I want to do is add some conditionals to the accessory based on those extension settings.

I’ve tried just calling this: $this->settings[‘variable’];

But if I use that to set a variable or do a conditional, I get errors like: “Uninitialized string offset” and “Cannot modify header information”.

What am I missing?

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
13 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

You’ll need to get the settings from the DB manually. Example (there are multiple ways):

$settings_query = $this->EE->db->select('settings')->where('class', 'Class_name_ext')->limit(1)->get('extensions');
$settings = ($settings_query->num_rows() > 0) ? $settings_query->result_array() : array() ;
       
Seamus's avatar
Seamus
47 posts
13 years ago
Seamus's avatar Seamus

Thanks so much for the tip Erik. I think it’s working as I can get settings to echo in the array, but it just spits out “Array”. I looked up result_array in the CodeIgniter docs and got even more confused.

I tried adding this after your code and that didn’t seem to work.

$highlight = $settings['highlight_color'];

Apologies for the novice questions, I’m sure this info is out there somewhere I just can’t seem to make it click.

       
Manuel Payano's avatar
Manuel Payano
144 posts
13 years ago
Manuel Payano's avatar Manuel Payano

Hi,

Accessories and extensions are different addon types. They don’t share any settings. In your accessory you will need to grab the extension settings yourself.

I am going to assume a couple of stuff: 1. Your extension class name: My_foo_ext 2. Same settings for all your extension hooks. (yes you can have different settings for each one, but thats another topic)

Directly from the DB

$settings = array();
$query = $this->EE->db->select('settings')->from('exp_extensions')->where('class', 'My_foo_ext')->limit(1)->get();

if ($query->num_rows() > 0)
{
 $settings = @unserialize($query->row('settings'));
 if ($settings ==== FALSE) $settings = array();
}

// $settings now holds your extension settings

I have not tested it for PHP errors. But should work fine.

Hope it helped!

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
13 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Ah, sorry. I was just typing in code there (without checking it). Change the result_array() bit to row_array().

       
Manuel Payano's avatar
Manuel Payano
144 posts
13 years ago
Manuel Payano's avatar Manuel Payano
You’ll need to get the settings from the DB manually. Example (there are multiple ways):
$settings_query = $this->EE->db->select('settings')->where('class', 'Class_name_ext')->limit(1)->get('extensions');
$settings = ($settings_query->num_rows() > 0) ? $settings_query->result_array() : array() ;

You beat me to it.. Don’t forget to unserialize() it!

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
13 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team
You beat me to it.. Don’t forget to unserialize() it!

Good call.

Seamus, I’d use this instead:

$settings_query = $this->EE->db->select('settings')->where('class', 'Class_name_ext')->limit(1)->get('extensions');
$settings = ($settings_query->num_rows() > 0) ? @unserialize($settings_query->row()->settings) : array() ;
       
Seamus's avatar
Seamus
47 posts
13 years ago
Seamus's avatar Seamus

Outstanding! It took a a little bit to figure out how to actually pull the right values with these snippets, but it works! I can hardly believe it.

Thank you both a hundred times over.

       
Focus Lab Dev Team's avatar
Focus Lab Dev Team
1,129 posts
13 years ago
Focus Lab Dev Team's avatar Focus Lab Dev Team

Glad you’re up and running 😊

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.