Add ability to identify a link to the current page

A new MenuItem boolean property is available, $is_active:
- its value is true if the link points to the current page
- its value is false otherwise
This commit is contained in:
vv221 2020-04-28 13:11:30 +02:00
parent c24b382c3b
commit 9e5c7a32e3

View file

@ -5,6 +5,11 @@ namespace dokuwiki\plugin\childrenpages;
use dokuwiki\Menu\Item\AbstractItem;
class MenuItem extends AbstractItem {
/**
* @var bool $is_active True if it is a link to the current page
*/
public bool $is_active;
/**
* Generate a menu item from a passed string
*
@ -24,6 +29,7 @@ class MenuItem extends AbstractItem {
$this->label = $label;
parent::__construct();
$this->setTargetFromType($type, $strip_namespace);
$this->setIsActive();
}
/**
@ -69,6 +75,16 @@ class MenuItem extends AbstractItem {
$this->params = [];
}
/**
* Set the active status of the link
*/
protected function setIsActive() : void {
global $INFO;
$current_page_id = $INFO['id'];
$target_page_id = $this->id;
$this->is_active = ( $target_page_id === $current_page_id );
}
/**
* Convenience method to get the attributes for constructing an <a> element
* Parent method is declared in dokuwiki\Menu\Item\AbstractItem