3 namespace dokuwiki\Menu
;
5 use dokuwiki\Extension\Event
;
6 use dokuwiki\Menu\Item\AbstractItem
;
11 * Basic menu functionality. A menu defines a list of AbstractItem that shall be shown.
12 * It contains convenience functions to display the menu in HTML, but template authors can also
13 * just accesst the items via getItems() and create the HTML as however they see fit.
15 abstract class AbstractMenu
implements MenuInterface
17 /** @var string[] list of Item classes to load */
18 protected $types = [];
20 /** @var int the context this menu is used in */
21 protected $context = AbstractItem
::CTX_DESKTOP
;
23 /** @var string view identifier to be set in the event */
27 * AbstractMenu constructor.
29 * @param int $context the context this menu is used in
31 public function __construct($context = AbstractItem
::CTX_DESKTOP
)
33 $this->context
= $context;
37 * Get the list of action items in this menu
39 * @return AbstractItem[]
40 * @triggers MENU_ITEMS_ASSEMBLY
42 public function getItems()
44 $data = ['view' => $this->view
, 'items' => []];
45 Event
::createAndTrigger('MENU_ITEMS_ASSEMBLY', $data, [$this, 'loadItems']);
46 return $data['items'];
50 * Default action for the MENU_ITEMS_ASSEMBLY event
52 * @param array $data The plugin data
55 public function loadItems(&$data)
57 foreach ($this->types
as $class) {
59 $class = "\\dokuwiki\\Menu\\Item\\$class";
60 /** @var AbstractItem $item */
62 if (!$item->visibleInContext($this->context
)) continue;
63 $data['items'][] = $item;
64 } catch (\RuntimeException
$ignored) {
71 * Generate HTML list items for this menu
73 * This is a convenience method for template authors. If you need more fine control over the
74 * output, use getItems() and build the HTML yourself
76 * @param string|false $classprefix create a class from type with this prefix, false for no class
77 * @param bool $svg add the SVG link
80 public function getListItems($classprefix = '', $svg = true)
83 foreach ($this->getItems() as $item) {
84 if ($classprefix !== false) {
85 $class = ' class="' . $classprefix . $item->getType() . '"';
90 $html .= "<li$class>";
91 $html .= $item->asHtmlLink(false, $svg);