summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog2
-rw-r--r--app/Helper/HookHelper.php27
-rw-r--r--doc/plugin-hooks.markdown8
-rw-r--r--tests/units/Helper/HookHelperTest.php51
4 files changed, 87 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index d013ad9a..25a92168 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -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
diff --git a/app/Helper/HookHelper.php b/app/Helper/HookHelper.php
index 418c55a0..e43cfdfd 100644
--- a/app/Helper/HookHelper.php
+++ b/app/Helper/HookHelper.php
@@ -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;
+ }
}
diff --git a/doc/plugin-hooks.markdown b/doc/plugin-hooks.markdown
index a700e34b..97816d5f 100644
--- a/doc/plugin-hooks.markdown
+++ b/doc/plugin-hooks.markdown
@@ -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.
diff --git a/tests/units/Helper/HookHelperTest.php b/tests/units/Helper/HookHelperTest.php
index 66d13381..a67eaed9 100644
--- a/tests/units/Helper/HookHelperTest.php
+++ b/tests/units/Helper/HookHelperTest.php
@@ -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