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 $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;
if ( empty($label) ) {
$label = ucfirst($type);
}
$this->label = $label;
parent::__construct();
$this->setTargetFromType();
$this->setTargetFromType($strip_namespace);
}
/**
* 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 $plugin_controller;
$language = null;
@ -36,16 +43,28 @@ class MenuItem extends AbstractItem {
$translation_plugin =& $plugin_controller->load('helper', 'translation');
$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 ( $language !== null && preg_match("/^$language:/", $INFO['id']) ) {
$this->id = preg_replace(
"/^$language:/",
"$language:".$this->type.':',
$INFO['id']
);
} else {
$this->id = $this->type.':'.$INFO['id'];
$is_in_a_lang_namespace = ( $language !== null && $page_path[0] === $language );
if ( $is_in_a_lang_namespace ) {
array_shift($page_path);
}
// 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 = [];
}
}

View file

@ -40,16 +40,20 @@ class action_plugin_childrenpages extends DokuWiki_Action_Plugin {
if ( $event->data['view'] !== 'page' ) {
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');
$top_namespace = $this->getTopLevelNamespace();
if ( in_array($top_namespace, $children_types) ) {
return;
$is_child_page = in_array($top_namespace, $children_types);
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
foreach ( $children_types as $child_type ) {
$child_label = $this->getLang("btn_$child_type");
$this->addMenuItem($event, $child_type, $child_label);
if ( $child_type !== $top_namespace ) {
$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 string $type
* @param string $name
* @param bool $strip_namespace
*/
protected function addMenuItem(Doku_Event $event, string $type, string $name = '') {
$item = $this->generateMenuItem($type, $name);
$event->data['items'][] = $item;
}
/**
* Generate a new menu item
*
* @param string $type
*/
protected function generateMenuItem(string $type, string $name = '') {
return new MenuItem($type, $name);
protected function addMenuItem(
Doku_Event $event,
string $type,
string $name = '',
bool $strip_namespace = false
) {
$event->data['items'][] = new MenuItem($type, $name, $strip_namespace);
}
/**

View file

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