Add the possibility to attach template hooks with a callback
This commit is contained in:
parent
2ebe8b3272
commit
010199e8f8
|
|
@ -3,7 +3,7 @@ Version 1.0.33 (unreleased)
|
|||
|
||||
Improvements:
|
||||
|
||||
* Add the possibility to attach template hooks with local variables
|
||||
* Add the possibility to attach template hooks with local variables and callback
|
||||
* Add "reference" hooks
|
||||
* Show project name in task forms
|
||||
* Convert vanilla CSS to SASS
|
||||
|
|
|
|||
|
|
@ -46,6 +46,12 @@ class HookHelper extends Base
|
|||
foreach ($this->hook->getListeners($hook) as $params) {
|
||||
if (! empty($params['variables'])) {
|
||||
$variables = array_merge($variables, $params['variables']);
|
||||
} elseif (! empty($params['callable'])) {
|
||||
$result = call_user_func_array($params['callable'], $variables);
|
||||
|
||||
if (is_array($result)) {
|
||||
$variables = array_merge($variables, $result);
|
||||
}
|
||||
}
|
||||
|
||||
$buffer .= $this->template->render($params['template'], $variables);
|
||||
|
|
@ -72,4 +78,25 @@ class HookHelper extends Base
|
|||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a template to a hook with a callable
|
||||
*
|
||||
* Arguments passed to the callback are the one passed to the hook
|
||||
*
|
||||
* @access public
|
||||
* @param string $hook
|
||||
* @param string $template
|
||||
* @param callable $callable
|
||||
* @return $this
|
||||
*/
|
||||
public function attachCallable($hook, $template, callable $callable)
|
||||
{
|
||||
$this->hook->on($hook, array(
|
||||
'template' => $template,
|
||||
'callable' => $callable,
|
||||
));
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -161,6 +161,14 @@ $this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/
|
|||
));
|
||||
```
|
||||
|
||||
Example to attach a template with a callable:
|
||||
|
||||
```php
|
||||
$this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar', function($hook_param1, $hook_param2) {
|
||||
return array('new_template_variable' => 'foobar'); // Inject a new variable into the plugin template
|
||||
});
|
||||
```
|
||||
|
||||
This call is usually defined in the `initialize()` method.
|
||||
The first argument is name of the hook and the second argument is the template name.
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,57 @@ use Kanboard\Helper\HookHelper;
|
|||
|
||||
class HookHelperTest extends Base
|
||||
{
|
||||
public function testAttachCallable()
|
||||
{
|
||||
$this->container['template'] = $this
|
||||
->getMockBuilder('\Kanboard\Core\Template')
|
||||
->setConstructorArgs(array($this->container['helper']))
|
||||
->setMethods(array('render'))
|
||||
->getMock();
|
||||
|
||||
$this->container['template']
|
||||
->expects($this->once())
|
||||
->method('render')
|
||||
->with(
|
||||
$this->equalTo('tpl1'),
|
||||
$this->equalTo(array('k0' => 'v0', 'k1' => 'v1'))
|
||||
)
|
||||
->will($this->returnValue('tpl1_content'));
|
||||
|
||||
$hookHelper = new HookHelper($this->container);
|
||||
$hookHelper->attachCallable('test', 'tpl1', function() {
|
||||
return array(
|
||||
'k1' => 'v1',
|
||||
);
|
||||
});
|
||||
|
||||
$this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0')));
|
||||
}
|
||||
|
||||
public function testAttachCallableWithNoResult()
|
||||
{
|
||||
$this->container['template'] = $this
|
||||
->getMockBuilder('\Kanboard\Core\Template')
|
||||
->setConstructorArgs(array($this->container['helper']))
|
||||
->setMethods(array('render'))
|
||||
->getMock();
|
||||
|
||||
$this->container['template']
|
||||
->expects($this->once())
|
||||
->method('render')
|
||||
->with(
|
||||
$this->equalTo('tpl1'),
|
||||
$this->equalTo(array('k0' => 'v0'))
|
||||
)
|
||||
->will($this->returnValue('tpl1_content'));
|
||||
|
||||
$hookHelper = new HookHelper($this->container);
|
||||
$hookHelper->attachCallable('test', 'tpl1', function() {
|
||||
});
|
||||
|
||||
$this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0')));
|
||||
}
|
||||
|
||||
public function testAttachLocalVariables()
|
||||
{
|
||||
$this->container['template'] = $this
|
||||
|
|
|
|||
Loading…
Reference in New Issue