Magento CMS – Use Helper Function in CMS Block

cmshelpertemplate-directivewysiwyg

I am trying to add helper function to cms block or page but it didn't parse like other directives.can anyone help to resolve this issue.or how make helper directive to work in cms block or cms page.

    <?php
class Mage_Customer_Helper_Data extends Mage_Core_Helper_Abstract
{
    public function useInWysiwygEditor{
    /* logic should be anything */
        return $this->getUrl('contact')
    }
}

now in cms editor i want to use like {{helper useInWysiwygEditor()}}

Thanks

Best Answer

I have created one custom module for this

and we have specified the our filter to magento by adding the following code in config.xml page

<global>
    <cms>
        <page>
            <tempate_filter>[Namespace]_[Module]_Model_Template_Filter</tempate_filter>
        </page>
        <block>
            <tempate_filter>[Namespace]_[Module]_Model_Template_Filter</tempate_filter>
        </block>
    </cms>
<global>

Then create the file [Namespace]/[Module]/Model/Template/Filter.php

class [Namespace]_[Module]_Model_Template_Filter extends Mage_Widget_Model_Template_Filter
{
    public function helperDirective($construction)
    {
        $params = $this->_getIncludeParameters($construction[2]);
        $allowedParams = array('module', 'method', 'params');
        if(empty($params) || !count(array_intersect($allowedParams, array_keys($params)))){
            return $construction[0];
        }
        if((isset($params['module']) && !empty($params['module'])) && (isset($params['module']) && !empty($params['module']))){
            try{
                $helper = Mage::helper($params['module']);
                if(is_callable(array($helper, $params['method']), true, $params['method'])){
                    $arg = array();
                    $method = $params['method'];
                    if(isset($params['params']) && !empty($params['params'])){
                        $arg = explode(',',$params['params']);
                    }
                    return call_user_func_array(array($helper, $method), $arg);
                }
            } catch (Exception $e){
                return $construction[0];
            }
        }
        return $construction[0];
    }
}

Note: here the function name is helperDirective. if you that to {{custom }} so the function name will customDirective (i.e,{{youname}}Directive)

Then I have use 3 parameters for this.

So you can call your helper function by following way

{{helper module="yourmodule" method="yourfunction" params="arg1,arg2,..etc"}}

here

module => 'module helper name'

method => 'function name'

params => 'parameters of the function'

I have created this module by following this tutorial