From 78ecdc05c34f706a4eab3ff09161a3a79189cdf6 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Mon, 21 Sep 2015 21:07:15 -0400 Subject: Add plugin hooks for assets --- app/Helper/Hook.php | 20 +++++++++++++++++++ app/Template/layout.php | 3 +++ doc/plugins.markdown | 29 ++++++++++++++++++++++++++++ tests/units/Helper/HookHelperTest.php | 36 ++++++++++++++++++++++++++++++++++- 4 files changed, 87 insertions(+), 1 deletion(-) diff --git a/app/Helper/Hook.php b/app/Helper/Hook.php index d7fe3d34..bf879878 100644 --- a/app/Helper/Hook.php +++ b/app/Helper/Hook.php @@ -10,6 +10,26 @@ namespace Helper; */ class Hook extends \Core\Base { + /** + * Add assets JS or CSS + * + * @access public + * @param string $type + * @param string $hook + * @param array $variables + * @return string + */ + public function asset($type, $hook) + { + $buffer = ''; + + foreach ($this->hook->getListeners($hook) as $file) { + $buffer .= $this->helper->asset->$type($file); + } + + return $buffer; + } + /** * Render all attached hooks * diff --git a/app/Template/layout.php b/app/Template/layout.php index 49ac2a08..cba8d2a3 100644 --- a/app/Template/layout.php +++ b/app/Template/layout.php @@ -21,6 +21,9 @@ asset->css('assets/css/print.css', true, 'print') ?> asset->customCss() ?> + hook->asset('css', 'template:layout:css') ?> + hook->asset('js', 'template:layout:js') ?> + diff --git a/doc/plugins.markdown b/doc/plugins.markdown index cccda796..1f04374f 100644 --- a/doc/plugins.markdown +++ b/doc/plugins.markdown @@ -154,6 +154,34 @@ List of merge hooks: - `$start` Calendar start date (string, ISO-8601 format) - `$end` Calendar end date (string, ISO-8601 format) +Asset Hooks +----------- + +Asset hooks can be used to add easily a new stylesheet or a new javascript file in the layout. You can use this feature to create a theme and override all Kanboard default styles. + +Example to add a new stylesheet: + +```php +hook->on('template:layout:css', 'plugins/Css/skin.css'); + } +} +``` + +List of asset Hooks: + +- `template:layout:css` +- `template:layout:js` + Template hooks -------------- @@ -338,3 +366,4 @@ Examples of plugins - [User timetable](https://github.com/kanboard/plugin-timetable) - [Subtask Forecast](https://github.com/kanboard/plugin-subtask-forecast) - [Theme plugin sample](https://github.com/kanboard/plugin-example-theme) +- [CSS plugin sample](https://github.com/kanboard/plugin-example-css) diff --git a/tests/units/Helper/HookHelperTest.php b/tests/units/Helper/HookHelperTest.php index 6661c90b..7745c674 100644 --- a/tests/units/Helper/HookHelperTest.php +++ b/tests/units/Helper/HookHelperTest.php @@ -37,4 +37,38 @@ class HookHelperTest extends Base $h->attach('test', 'tpl2'); $this->assertEquals('tpl1_contenttpl2_content', $h->render('test')); } -} \ No newline at end of file + + public function testAssetHooks() + { + $this->container['helper']->asset = $this + ->getMockBuilder('\Helper\Asset') + ->setConstructorArgs(array($this->container)) + ->setMethods(array('css', 'js')) + ->getMock(); + + $this->container['helper'] + ->asset + ->expects($this->at(0)) + ->method('css') + ->with( + $this->equalTo('skin.css') + ) + ->will($this->returnValue('')); + + $this->container['helper'] + ->asset + ->expects($this->at(1)) + ->method('js') + ->with( + $this->equalTo('skin.js') + ) + ->will($this->returnValue('')); + + $h = new Hook($this->container); + $h->attach('test1', 'skin.css'); + $h->attach('test2', 'skin.js'); + + $this->assertContains('', $h->asset('css', 'test1')); + $this->assertContains('', $h->asset('js', 'test2')); + } +} -- cgit v1.2.3