Issue
in Cakephp 3 im trying to implement events in the helper class, this is example of what im trying to do:
protected $_View;
public function __construct(View $View, $config = [])
{
//debug($View);die;
$this->_View = $View;
parent::__construct($View, $config);
$this->_setupEvents();
}
/**
* setup events
*/
protected function _setupEvents() {
$events = [
'filter' => [$this, 'filter'],
];
foreach ($events as $callable) {
$this->_View->eventManager()->on("Helper.Layout.beforeFilter", $callable);
}
}
public function filter(&$content, $options = array()) {
preg_match_all('/\[(menu|m):([A-Za-z0-9_\-]*)(.*?)\]/i', $content, $tagMatches);
for ($i = 0, $ii = count($tagMatches[1]); $i < $ii; $i++) {
$regex = '/(\S+)=[\'"]?((?:.(?![\'"]?\s+(?:\S+)=|[>\'"]))+.)[\'"]?/i';
preg_match_all($regex, $tagMatches[3][$i], $attributes);
$menuAlias = $tagMatches[2][$i];
$options = array();
for ($j = 0, $jj = count($attributes[0]); $j < $jj; $j++) {
$options[$attributes[1][$j]] = $attributes[2][$j];
}
$content = str_replace($tagMatches[0][$i], $this->menu($menuAlias, $options), $content);
}
return $content;
}
But im getting warning for the line where im calling constructor of parent Helper class:
Warning (4096): Argument 1 passed to App\View\Helper\MenusHelper::__construct() must be an instance of App\View\Helper\View, instance of App\View\AppView given, called in C:\wamp\www\CookieCMS\vendor\cakephp\cakephp\src\View\HelperRegistry.php on line 142 and defined [APP/View\Helper\MenusHelper.php, line 26]
Is it possible to implement events in helper this way and what is it that im doing wrong?
Solution
The Helper class is already implements EventlistenerInterface, so as explained in the manual all you have to do in your custom helper is return a proper array with required event name to callback mapping from implementedEvents() method.
So something like:
public function implementedEvents()
{
$mapping = parent::implementedEvents();
$mapping += [
'Helper.Layout.beforeFilter' => 'someMethodOfYourHelper',
];
return $mapping;
}
Answered By - ADmad
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.