Search code examples
phpcakephpplugins

Multiple JS Engine Helpers cakephp 2.x


I have two plugins that both need to extend the JqueryEngineHelper.

I know that you can only specify one engine in AppController Helpers.

How can I extend the JqueryEngineHelper in both plugins? I have them both working but I cannot get them to work at the same time.

$helpers = ['Js'=>['MyPlugin.MyPluginJquery', 'MyPlugin2.MyPlugin2Jquery']];

I would like both to work, but unfortunately they do not. Only the first one is used.

Code from one of the engines

App::uses('AppHelper', 'View/Helper');
App::uses('JqueryEngineHelper', 'View/Helper');

class MrgCustomSelectJqueryEngineHelper extends JqueryEngineHelper{

    function __construct(View $view, $settings = array()){
        parent::__construct($view, $settings = array());

        $this->_init_callbacks();
    }

    protected function _init_callbacks(){
        $callbacks = [
            'selectBoxIt'=>[]
        ];
        $this->_callbackArguments = array_merge($this->_callbackArguments, $callbacks);
    }


    public function selectBoxIt($options = []){
        $template = '%s.selectBoxIt({%s});';
        return $this->_methodTemplate('selectBoxIt', $template, $options);
    }
}

Solution

  • I think you're going to have to alter the plugins so that they don't conflict. Depending on how the plugins work, there may be different ways you can go about this:

    1. Only load the version Js helper you need at the moment. Only works if you don't need both for a single controller action.
    2. Change the plugins to refer to the different versions of Js helper by different names. This could possibly take a lot of search and replace.
    3. If there are no conflicts between how the plugins extended Js helper, you can merge the two helpers.