1

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

asked Jul 2, 2015 at 8:35
2
  • can you show the code ? Commented Jul 2, 2015 at 8:36
  • please explain question Commented Jul 2, 2015 at 8:56

1 Answer 1

5

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

answered Jul 3, 2015 at 9:21
1
  • Its working perfectly mate.Thanks. But i confused about tempate_filter i thought it should be template_filter :) Commented Nov 15, 2018 at 7:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.