From eefa64ecb41250803e1b7ee9a81511963dd2e997 Mon Sep 17 00:00:00 2001 From: vv221 Date: Fri, 3 Apr 2020 01:15:41 +0200 Subject: [PATCH] Generate new menu items from a hardcoded list of children --- MenuItem.php | 18 ++++++++++++++++++ action.php | 32 +++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 MenuItem.php diff --git a/MenuItem.php b/MenuItem.php new file mode 100644 index 0000000..1d48e55 --- /dev/null +++ b/MenuItem.php @@ -0,0 +1,18 @@ +type = $type; + parent::__construct(); + trigger_error("generating a menu item for type \"$this->type\" not implemented in ".get_class($this), E_USER_WARNING); + } +} diff --git a/action.php b/action.php index 2d54d5b..202bff9 100644 --- a/action.php +++ b/action.php @@ -11,6 +11,8 @@ if ( ! defined('DOKU_INC') ) { die(); } +use dokuwiki\plugin\childrenpages\MenuItem; + class action_plugin_childrenpages extends DokuWiki_Action_Plugin { /** * Registers a callback function for a given event @@ -18,7 +20,7 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { * @param Doku_Event_Handler $controller */ public function register(Doku_Event_Handler $controller) : void { - $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuItem'); + $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addMenuItems'); } /** @@ -26,7 +28,7 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { * * @param Doku_Event $event */ - public function addMenuItem(Doku_Event $event) : void { + public function addMenuItems(Doku_Event $event) : void { global $INFO; // Check that this method has been called in the expected context if ( $event->name !== 'MENU_ITEMS_ASSEMBLY' ) { @@ -43,6 +45,30 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { if ( ! empty($INFO['namespace']) ) { return; } - trigger_error('addMenuItem() not implemented in '.get_class($this), E_USER_WARNING); + // Get the list of children pages + $children = [ 'animation', 'gameplay', 'dev', 'talk' ]; + foreach ( $children as $child ) { + $this->addMenuItem($event, $child); + } + } + + /** + * Add a new item to the page menu + * + * @param Doku_Event $event + * @param string $child + */ + protected function addMenuItem(Doku_Event $event, string $child) { + $item = $this->generateMenuItem($child); + $event->data['items'][] = $item; + } + + /** + * Generate a new menu item + * + * @param string $type + */ + protected function generateMenuItem(string $type) { + return new MenuItem($type); } }