diff options
-rw-r--r-- | ChangeLog | 1 | ||||
-rw-r--r-- | app/Helper/HookHelper.php | 25 | ||||
-rw-r--r-- | doc/plugin-hooks.markdown | 8 | ||||
-rw-r--r-- | tests/units/Helper/HookHelperTest.php | 40 |
4 files changed, 57 insertions, 17 deletions
@@ -3,6 +3,7 @@ Version 1.0.33 (unreleased) Improvements: +* Add the possibility to attach template hooks with local variables * Add "reference" hooks * Show project name in task forms * Convert vanilla CSS to SASS diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php index cb4dc1ef..418c55a0 100644 --- a/app/Helper/HookHelper.php +++ b/app/Helper/HookHelper.php @@ -24,8 +24,8 @@ class HookHelper extends Base { $buffer = ''; - foreach ($this->hook->getListeners($hook) as $file) { - $buffer .= $this->helper->asset->$type($file); + foreach ($this->hook->getListeners($hook) as $params) { + $buffer .= $this->helper->asset->$type($params['template']); } return $buffer; @@ -43,8 +43,12 @@ class HookHelper extends Base { $buffer = ''; - foreach ($this->hook->getListeners($hook) as $template) { - $buffer .= $this->template->render($template, $variables); + foreach ($this->hook->getListeners($hook) as $params) { + if (! empty($params['variables'])) { + $variables = array_merge($variables, $params['variables']); + } + + $buffer .= $this->template->render($params['template'], $variables); } return $buffer; @@ -54,13 +58,18 @@ class HookHelper extends Base * Attach a template to a hook * * @access public - * @param string $hook - * @param string $template + * @param string $hook + * @param string $template + * @param array $variables * @return $this */ - public function attach($hook, $template) + public function attach($hook, $template, array $variables = array()) { - $this->hook->on($hook, $template); + $this->hook->on($hook, array( + 'template' => $template, + 'variables' => $variables, + )); + return $this; } } diff --git a/doc/plugin-hooks.markdown b/doc/plugin-hooks.markdown index 9a4bdab2..a700e34b 100644 --- a/doc/plugin-hooks.markdown +++ b/doc/plugin-hooks.markdown @@ -153,6 +153,14 @@ Example to add new content in the dashboard sidebar: $this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar'); ``` +Example to attach a template with local variables: + +```php +$this->template->hook->attach('template:dashboard:sidebar', 'myplugin:dashboard/sidebar', array( + 'variable' => 'foobar', +)); +``` + 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. diff --git a/tests/units/Helper/HookHelperTest.php b/tests/units/Helper/HookHelperTest.php index 6e03acd1..66d13381 100644 --- a/tests/units/Helper/HookHelperTest.php +++ b/tests/units/Helper/HookHelperTest.php @@ -6,6 +6,28 @@ use Kanboard\Helper\HookHelper; class HookHelperTest extends Base { + public function testAttachLocalVariables() + { + $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->attach('test', 'tpl1', array('k1' => 'v1')); + $this->assertEquals('tpl1_content', $hookHelper->render('test', array('k0' => 'v0'))); + } + public function testMultipleHooks() { $this->container['template'] = $this @@ -32,10 +54,10 @@ class HookHelperTest extends Base ) ->will($this->returnValue('tpl2_content')); - $h = new HookHelper($this->container); - $h->attach('test', 'tpl1'); - $h->attach('test', 'tpl2'); - $this->assertEquals('tpl1_contenttpl2_content', $h->render('test')); + $hookHelper = new HookHelper($this->container); + $hookHelper->attach('test', 'tpl1'); + $hookHelper->attach('test', 'tpl2'); + $this->assertEquals('tpl1_contenttpl2_content', $hookHelper->render('test')); } public function testAssetHooks() @@ -64,11 +86,11 @@ class HookHelperTest extends Base ) ->will($this->returnValue('<script src="skin.js"></script>')); - $h = new HookHelper($this->container); - $h->attach('test1', 'skin.css'); - $h->attach('test2', 'skin.js'); + $hookHelper = new HookHelper($this->container); + $hookHelper->attach('test1', 'skin.css'); + $hookHelper->attach('test2', 'skin.js'); - $this->assertContains('<link rel="stylesheet" href="skin.css"></link>', $h->asset('css', 'test1')); - $this->assertContains('<script src="skin.js"></script>', $h->asset('js', 'test2')); + $this->assertContains('<link rel="stylesheet" href="skin.css"></link>', $hookHelper->asset('css', 'test1')); + $this->assertContains('<script src="skin.js"></script>', $hookHelper->asset('js', 'test2')); } } |