0

Is there any way to pass a value from an extension to a module or a plugin in ExpressionEngine?

I created this very useful extension to take advantage of the expresso store module:

<?php

use Store\Model\Order;
use Store\Model\OrderShippingMethod;

class Credit_tracker_ext
{

    protected $ee;
    public $name        = 'Credit tracker extention';
    public $settings_exist = 'n';
    public $hooks          = array('store_checkout_form_validation', 'store_order_shipping_methods');

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

    public function activate_extension()
    {
        foreach ($this->hooks as $hook) {
            $data = array(
                'class'    => __CLASS__,
                'method'   => $hook,
                'hook'     => $hook,
                'settings' => serialize($this->settings),
                'priority' => 10,
                'version'  => $this->version,
                'enabled'  => 'y',
            );
            ee()->db->insert('extensions', $data);
        }
    }

    public function disable_extension()
    {
        ee()->db->where('class', __CLASS__);
        ee()->db->delete('extensions');
    }
    public function settings()
    {

    }

    public function store_checkout_form_validation($str, $obj)
    {

    }

    public function store_order_shipping_methods(Order $order, array $obj)
    {
        if (ee()->extensions->last_call !== false) {
            $methods = ee()->extensions->last_call;
        }


        $option = [];
        foreach ($order->items as $value) {

            $option = new OrderShippingMethod;
            $option->id = $value->entry_id;
        }

        $methods[$option->id] = $option;

        var_dump($methods);

        return $methods;
    }
}

I'd like to set up the object $method as a global array, to access it from another file, in another moment.

To clarify this point, the extension in this system is called at a certain point of the code, and the function that fires the extension is called hook, like in WordPress.

So essentially, I'd like to call the variable that I returned within another file (for example mod.credit_traker.php), where I can simply call the variable $methods.

Anyone who had this problem before?

1 Answer 1

0

Well, unless you have a process chain between your extension and your module and your plugin (one calls all the others, creates accessible in memory objects, something like that), nope, not in a single process. This is not a real thing you can do.

This is where you use the database. If you have a module, you can setup database tables that are created when you install the module and removed when you uninstall the module. This would be where you create your data-store model for your entire add-on package. Then you just access the database from your different add-on components.

In this case, you install the entire add-on as a package and create your database tables. Then, when your extension is fired, it can store data in your database tables as needed, which can be recalled by the plugin or the module.

You might want to consider employing an actual Model element that can be accessed by your add-on components instead of actually querying the database and inserting to the database for each add-on component. Think like, MVC style Model, a reusable class that interacts with your data-store for you.

3
  • What about this process chain? How does it work? Jun 22, 2018 at 4:12
  • Oh, I didn't really state it properly... it isn't an option. I mean, it is, with extreme complexity added and probably a secondary running process or even more. The answer is absolutely utilize creating a database table for your add-on that acts a data-store. That is 100% the correct way to treat persistent storage for an EE add-on, and for all PHP apps in general.
    – jrothafer
    Jun 22, 2018 at 5:35
  • Fair enough, ok I will deal with the first option Jun 22, 2018 at 18:43

Your Answer

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

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