On children pages, show links to other children pages and back to the main parent page

This commit is contained in:
vv221 2020-04-03 22:37:30 +02:00
parent e137f57890
commit 83bba9954a
3 changed files with 49 additions and 28 deletions

View file

@ -10,21 +10,28 @@ class MenuItem extends AbstractItem {
* *
* @param string $type * @param string $type
* @param string $label * @param string $label
* @param bool $strip_namespace
*/ */
public function __construct(string $type, string $label = '') { public function __construct(
string $type,
string $label = '',
bool $strip_namespace = false
) {
$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();
$this->setTargetFromType(); $this->setTargetFromType($strip_namespace);
} }
/** /**
* Set the item target link from its type * Set the item target link from its type
*
* @param bool $strip_namespace
*/ */
protected function setTargetFromType() : void { protected function setTargetFromType(bool $strip_namespace) : void {
global $INFO; global $INFO;
global $plugin_controller; global $plugin_controller;
$language = null; $language = null;
@ -36,16 +43,28 @@ class MenuItem extends AbstractItem {
$translation_plugin =& $plugin_controller->load('helper', 'translation'); $translation_plugin =& $plugin_controller->load('helper', 'translation');
$language = $translation_plugin->getLangPart($INFO['id']); $language = $translation_plugin->getLangPart($INFO['id']);
} }
// Split path to page
$page_path = explode(':', $INFO['id']);
// If the top level namespace is a language one, the children namespace should be inserted inside it // If the top level namespace is a language one, the children namespace should be inserted inside it
if ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) { $is_in_a_lang_namespace = ( $language !== null && $page_path[0] === $language );
$this->id = preg_replace( if ( $is_in_a_lang_namespace ) {
"/^$language:/", array_shift($page_path);
"$language:".$this->type.':',
$INFO['id']
);
} else {
$this->id = $this->type.':'.$INFO['id'];
} }
// Strip the top level namespace if we are already on a child page
if ( $strip_namespace ) {
array_shift($page_path);
}
// Build the link target path
$target_path = [];
if ( $is_in_a_lang_namespace ) {
$target_path[] = $language;
}
// Add namespace of the target child page, unless the special value "_main" has been used
// "_main" is used to generate a link back to the main parent page
if ( $this->type !== '_main' ) {
$target_path[] = $this->type;
}
$this->id = implode(':', array_merge($target_path, $page_path));
$this->params = []; $this->params = [];
} }
} }

View file

@ -40,16 +40,20 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
if ( $event->data['view'] !== 'page' ) { if ( $event->data['view'] !== 'page' ) {
return; return;
} }
// Only add links if the current page is not included in a reserved namespace // If the current page is included in a reserved namespace, add a link back to main page
$children_types = $this->getConf('children_list'); $children_types = $this->getConf('children_list');
$top_namespace = $this->getTopLevelNamespace(); $top_namespace = $this->getTopLevelNamespace();
if ( in_array($top_namespace, $children_types) ) { $is_child_page = in_array($top_namespace, $children_types);
return; if ( $is_child_page ) {
$main_label = $this->getLang('btn_main');
$this->addMenuItem($event, '_main', $main_label, $top_namespace);
} }
// Add menu items for each child page // Add menu items for each child page
foreach ( $children_types as $child_type ) { foreach ( $children_types as $child_type ) {
$child_label = $this->getLang("btn_$child_type"); if ( $child_type !== $top_namespace ) {
$this->addMenuItem($event, $child_type, $child_label); $child_label = $this->getLang("btn_$child_type");
$this->addMenuItem($event, $child_type, $child_label, $is_child_page);
}
} }
} }
@ -59,19 +63,15 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
* @param Doku_Event $event * @param Doku_Event $event
* @param string $type * @param string $type
* @param string $name * @param string $name
* @param bool $strip_namespace
*/ */
protected function addMenuItem(Doku_Event $event, string $type, string $name = '') { protected function addMenuItem(
$item = $this->generateMenuItem($type, $name); Doku_Event $event,
$event->data['items'][] = $item; string $type,
} string $name = '',
bool $strip_namespace = false
/** ) {
* Generate a new menu item $event->data['items'][] = new MenuItem($type, $name, $strip_namespace);
*
* @param string $type
*/
protected function generateMenuItem(string $type, string $name = '') {
return new MenuItem($type, $name);
} }
/** /**

View file

@ -1,5 +1,7 @@
<?php <?php
$lang['btn_main'] = 'Back to main page';
$lang['btn_animation'] = 'Animation'; $lang['btn_animation'] = 'Animation';
$lang['btn_dev'] = 'Dev'; $lang['btn_dev'] = 'Dev';
$lang['btn_gameplay'] = 'Gameplay'; $lang['btn_gameplay'] = 'Gameplay';