0

I have written a custom add-on which consists of an extension and a plugin.

The extension basically loops through a set of channel entries, gets some matrix data and performs a calculation based on the contents of a grid field and then stores that information within the the entry. This all works great.

The plugin uses the calculation method of the extension and works OK except for one critical element.

The extension features a setting for a "multiplier value" which it uses in its calculation. Everything appears to be fine with this and if I change the value in the extension's settings the resulting figures are correct.

However, the plugin displays a PHP Warning about an Illegal string offset for this multiplier setting. If I hardcode the setting within the extension or remove that part of the calculation then again it works OK.

The problem seems to be the way the extension accesses the setting value. I'm guessing that when used internally it manages to muddle through and works but is fundamentally done incorrectly.

I've got the following within my extension;

function settings() {
        $settings = array();
        $settings['points_multiplier']      = array('i', '', "1"); // default value is 1
        return $settings;
    } // END

And the method that does the calculation returns a value multiplied by that setting...

// use points multiplier (default is 1)
$points_multiplier = $this->settings['points_multiplier']; 
$total_credits = $total_credits * $points_multiplier;
return $total_credits;

If I dump out the value of $points_multiplier or $this->settings['points_multiplier'] then I get the value expected (although the latter outputs a string).

As far as I can tell, I've done the settings bit correctly.

So why does my plugin throw this warning and ultimately not work?

UPDATE Rob Sanchez's answer fixed the problem but for the sake of completeness, here is the code that I was using in my plugin.

$ext = new Fm_credits_from_entry_ext();

$query = $this->EE->db->select('entry_id')->where('author_id',$this->author_id)->where('channel_id','7')->where('status',$status)->get('channel_titles');       

if($query->num_rows() > 0){
    foreach($query->result_array() as $row){
        $credits += $ext->calculate_total_credits($row['entry_id']);
    }
}

The missing nugget of information was that when the extension is loaded by the plugin it is necessary to manually load the settings for that extension.

1
  • Can you post the code you're using to access the extension method from inside your plugin? Commented Oct 2, 2014 at 12:49

3 Answers 3

3

Judging from this statement: "The plugin uses the calculation method of the extension", I assume you're doing something like this in your plugin:

$ext = new Your_addon_ext();

return $ext->do_calculation(ee()->TMPL->fetch_param('your_value'));

In this scenario, the extension object will not automatically load its settings, like it normally does when EE runs its hooks. You have to explicitly load them yourself from the database. I suggest adding a method to your extension to load settings.

public function load_settings()
{
    if (empty($this->settings))
    {
        $query = ee()->db->select('settings')
            ->where('class', __CLASS__)
            ->limit(1)
            ->get('extensions');

        $this->settings = unserialize($query->row('settings'));
    }
}

Then you can call this in your plugin:

$ext = new Your_addon_ext();

// load the extension settings first
$ext->load_settings();

return $ext->do_calculation(ee()->TMPL->fetch_param('your_value'));
2
  • That looks really promising! I'll give it a go in the morning. You are correct in thinking I was loading the extension into the plugin and then calling one of its methods. The setting in question is used in that method (I don't use it explicitly) but what you said makes sense.
    – foamcow
    Commented Oct 2, 2014 at 16:02
  • I couldn't wait! I tried it and it worked perfectly. Thank you, that's filled a gap in my knowledge.
    – foamcow
    Commented Oct 2, 2014 at 16:12
1

To get the value of 1 from your $settings['points_multiplier']; wouldn't you need to reference it as $settings['points_multiplier'][2] since it's an array ?

Or am I missing something?

1
  • I tried this with no success. I hear what you're saying but using $this->settings['points_multiplier'] gets the correct value (if I change the setting it is reflected in the calculations and if I dump the value out). Surely the array is $settings and it's associative, so ['points_multiplier'] is a key and we get the value associated with it?
    – foamcow
    Commented Oct 2, 2014 at 11:59
0

Have you tried casting the value of $this->settings['points_multiplier'] to an integer?

$points_multiplier = (int) $this->settings['points_multiplier']; 
2
  • Yes. Tried that. Also as (float). It's weird because everything is fine within the extension. It's only when I try to use one of its methods from the plugin that I get this problem.
    – foamcow
    Commented Oct 2, 2014 at 13:36
  • Definitely sounds weird. If you var_dump the value that you have casted, what does it return? What is the value of $total_credits?
    – joedixon
    Commented Oct 2, 2014 at 13:43

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.