From 98441884697ce340e7bd9b97619324f4f80caa84 Mon Sep 17 00:00:00 2001 From: vv221 Date: Fri, 3 Apr 2020 05:32:33 +0200 Subject: [PATCH] Improve handling of compatibility with "translate" plugin --- MenuItem.php | 15 +++++++++++---- action.php | 35 ++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/MenuItem.php b/MenuItem.php index 5b14b6c..2362e12 100644 --- a/MenuItem.php +++ b/MenuItem.php @@ -12,17 +12,24 @@ class MenuItem extends AbstractItem { * @param string $label */ public function __construct(string $type, string $label = '') { - global $INFO; $this->type = $type; if ( empty($label) ) { $label = ucfirst($type); } $this->label = $label; parent::__construct(); - // Edit the item to show a link to the requested children page - if ( ! plugin_isdisabled('translate') ) { + $this->setTargetFromType(); + } + + /** + * Set the item target link from its type + */ + protected function setTargetFromType() : void { + global $INFO; + global $plugin_controller; + if ( ! $plugin_controller->isdisabled('translate') ) { // If the "translate" plugin is activated, the language code should stay the top-level namespace - $translate_plugin =& plugin_load('helper', 'translate'); + $translate_plugin =& $plugin_controller->load('helper', 'translate'); $language = $translate_plugin->getPageLanguage(); if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) { $this->id = preg_replace( diff --git a/action.php b/action.php index 23ae1ee..d64ccf8 100644 --- a/action.php +++ b/action.php @@ -29,7 +29,6 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { * @param Doku_Event $event */ 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' ) { $message = "Tabpage plugin error:"; @@ -43,18 +42,7 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { } // Only add links if the current page is not included in a reserved namespace $children_types = $this->getConf('children_list'); - if ( ! plugin_isdisabled('translate') ) { - // Skip top-level namespace added by "translate" plugin - $translate_plugin =& plugin_load('helper', 'translate'); - $language = $translate_plugin->getPageLanguage(); - if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) { - $top_namespace = explode(':', $INFO['namespace'])[1]; - } else { - $top_namespace = explode(':', $INFO['namespace'])[0]; - } - } else { - $top_namespace = explode(':', $INFO['namespace'])[0]; - } + $top_namespace = $this->getTopLevelNamespace(); if ( in_array($top_namespace, $children_types) ) { return; } @@ -85,4 +73,25 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin { protected function generateMenuItem(string $type, string $name = '') { return new MenuItem($type, $name); } + + /** + * Get the top level namespace for the current page + * Exclude namespace added by the "translate" plugin + */ + protected function getTopLevelNamespace() : ?string { + global $INFO; + global $plugin_controller; + if ( ! $plugin_controller->isdisabled('translate') ) { + // Skip top-level namespace added by "translate" plugin + $translate_plugin =& $plugin_controller->load('helper', 'translate'); + $language = $translate_plugin->getPageLanguage(); + if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) { + return explode(':', $INFO['namespace'])[1]; + } else { + return explode(':', $INFO['namespace'])[0]; + } + } else { + return explode(':', $INFO['namespace'])[0]; + } + } }