Search code examples
phptwitter-bootstrapcakephphelpernon-member-functions

CakePHP 3 - call object from other helper


I'm actually working on a Helper for CakePHP3 that include BsHelper and then the BsFormHelper.

Actually everything looks good, no problem with Bootstrap formats.

I try now to create a ckEditor instance, but I meet some several problems.

If i try to call my ckEditor like this :

$this->BsForm->ckEditor('test')

I just have some problems because the function ckEditor is in my BsFormHelper, and load function is in BsHelper. So when i try to access private var to know if i had to load ckEditor i got that issue :

Error: Call to a member function load() on a non-object File C:\wamp3\www\wac_lucien\BsHelpersCakePHP3\3.2\plugins\BsHelpers\src\View\Helper\BsFormHelper.php

So in fact I know where is the issue :

In BsFormHelper my fonction looks like :

public function ckEditor($fieldName, $options = array(), $ckEditorOptions = array()) {
    $options['type'] = 'textarea';

    $out = $this->input($fieldName, $options);

    // If there is a point in the fieldName
    if (strpos($fieldName, '.') !== false) {
        $nameForReplace = Inflector::camelize(Inflector::slug($fieldName));
    } else {
        $nameForReplace = $this->_modelForm . Inflector::camelize($fieldName);
    }

    $this->Bs->load('ckeditor');

    $this->Bs->loadJS('CKEDITOR.replace("' . $nameForReplace . '", ' . json_encode($ckEditorOptions) . ');', true);
    return $out;
}

And in my BsHelper i got :

public function load($key) {
    if (!$this->__extensions[$key]['loaded']) {
        foreach ($this->__extensions[$key]['css'] as $css) {
            $this->loadCSS($css);
        }
        foreach ($this->__extensions[$key]['js'] as $js) {
            $this->loadJS($js);
        }
        $this->__extensions[$key]['loaded'] = true;
    }

    return $this->__extensions[$key]['loaded'];
}

Values are in declaration like this

public $__extensions = array(
    'jasny' => array(
        'css' => array(
            '//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/css/jasny-bootstrap.min.css'
        ),
        'js' => array(
            '//cdnjs.cloudflare.com/ajax/libs/jasny-bootstrap/3.1.3/js/jasny-bootstrap.min.js'
        ),
        'loaded' => true
    ),
    'ckeditor' => array(
        'css' => array(),
        'js' => array(
            '//cdn.ckeditor.com/4.5.8/standard/ckeditor.js'
        ),
        'loaded' => true
    )
);

Can someone help me to find out ? It looks like load function called in BsFormHelper can't access privates vars from BsHelper ...


Solution

  • seems you are just trying to use a helper in another helper

    The manual says

    You may wish to use some functionality already existing in another helper. To do so, you can specify helpers you wish to use with a $helpers array, formatted just as you would in a controller:

    So in your BsFormHelper just do

    public $helpers = ['Bs'];
    

    and you're done