dokuwiki-plugin-childrenpages/MenuItem.php
vv221 91c198cf0a Add a prefix to the type of menu items generated by this plugin
The prefix used is "childrenpages_".
This should make it easier for template builders to identify the menu
items added by this plugin.
2020-04-05 15:25:56 +02:00

71 lines
2.2 KiB
PHP

<?php
namespace dokuwiki\plugin\childrenpages;
use dokuwiki\Menu\Item\AbstractItem;
class MenuItem extends AbstractItem {
/**
* Generate a menu item from a passed string
*
* @param string $type
* @param string $label
* @param bool $strip_namespace
*/
public function __construct(
string $type,
string $label = '',
bool $strip_namespace = false
) {
$this->type = "childrenpages_$type";
if ( empty($label) ) {
$label = ucfirst($type);
}
$this->label = $label;
parent::__construct();
$this->setTargetFromType($type, $strip_namespace);
}
/**
* Set the item target link from its type
*
* @param string $type
* @param bool $strip_namespace
*/
protected function setTargetFromType(string $type, bool $strip_namespace) : void {
global $INFO;
global $plugin_controller;
$language = null;
// If one of the "translate" or "translation" plugins is activated, get the language code for the current page
if ( ! $plugin_controller->isdisabled('translate') ) {
$translate_plugin =& $plugin_controller->load('helper', 'translate');
$language = $translate_plugin->getPageLanguage();
} elseif ( ! $plugin_controller->isdisabled('translation') ) {
$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
$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[] = $type;
}
$this->id = implode(':', array_merge($target_path, $page_path));
$this->params = [];
}
}