dokuwiki-plugin-childrenpages/MenuItem.php

52 lines
1.5 KiB
PHP
Raw Normal View History

<?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
*/
public function __construct(string $type, string $label = '') {
$this->type = $type;
if ( empty($label) ) {
$label = ucfirst($type);
}
$this->label = $label;
parent::__construct();
$this->setTargetFromType();
}
/**
* Set the item target link from its type
*/
protected function setTargetFromType() : 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']);
}
// 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'];
}
$this->params = [];
}
}