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:
parent
c24b382c3b
commit
9e5c7a32e3
1 changed files with 16 additions and 0 deletions
16
MenuItem.php
16
MenuItem.php
|
@ -5,6 +5,11 @@ namespace dokuwiki\plugin\childrenpages;
|
||||||
use dokuwiki\Menu\Item\AbstractItem;
|
use dokuwiki\Menu\Item\AbstractItem;
|
||||||
|
|
||||||
class MenuItem extends 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
|
* Generate a menu item from a passed string
|
||||||
*
|
*
|
||||||
|
@ -24,6 +29,7 @@ class MenuItem extends AbstractItem {
|
||||||
$this->label = $label;
|
$this->label = $label;
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
$this->setTargetFromType($type, $strip_namespace);
|
$this->setTargetFromType($type, $strip_namespace);
|
||||||
|
$this->setIsActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -69,6 +75,16 @@ class MenuItem extends AbstractItem {
|
||||||
$this->params = [];
|
$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
|
* Convenience method to get the attributes for constructing an <a> element
|
||||||
* Parent method is declared in dokuwiki\Menu\Item\AbstractItem
|
* Parent method is declared in dokuwiki\Menu\Item\AbstractItem
|
||||||
|
|
Loading…
Reference in a new issue