Improve handling of compatibility with "translate" plugin

This commit is contained in:
vv221 2020-04-03 05:32:33 +02:00
parent 32290a00d1
commit 9844188469
2 changed files with 33 additions and 17 deletions

View file

@ -12,17 +12,24 @@ class MenuItem extends AbstractItem {
* @param string $label * @param string $label
*/ */
public function __construct(string $type, string $label = '') { public function __construct(string $type, string $label = '') {
global $INFO;
$this->type = $type; $this->type = $type;
if ( empty($label) ) { if ( empty($label) ) {
$label = ucfirst($type); $label = ucfirst($type);
} }
$this->label = $label; $this->label = $label;
parent::__construct(); parent::__construct();
// Edit the item to show a link to the requested children page $this->setTargetFromType();
if ( ! plugin_isdisabled('translate') ) { }
/**
* 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 // 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(); $language = $translate_plugin->getPageLanguage();
if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) { if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) {
$this->id = preg_replace( $this->id = preg_replace(

View file

@ -29,7 +29,6 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
* @param Doku_Event $event * @param Doku_Event $event
*/ */
public function addMenuItems(Doku_Event $event) : void { public function addMenuItems(Doku_Event $event) : void {
global $INFO;
// Check that this method has been called in the expected context // Check that this method has been called in the expected context
if ( $event->name !== 'MENU_ITEMS_ASSEMBLY' ) { if ( $event->name !== 'MENU_ITEMS_ASSEMBLY' ) {
$message = "Tabpage plugin error:"; $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 // Only add links if the current page is not included in a reserved namespace
$children_types = $this->getConf('children_list'); $children_types = $this->getConf('children_list');
if ( ! plugin_isdisabled('translate') ) { $top_namespace = $this->getTopLevelNamespace();
// 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];
}
if ( in_array($top_namespace, $children_types) ) { if ( in_array($top_namespace, $children_types) ) {
return; return;
} }
@ -85,4 +73,25 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
protected function generateMenuItem(string $type, string $name = '') { protected function generateMenuItem(string $type, string $name = '') {
return new MenuItem($type, $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];
}
}
} }