3 namespace Drupal\Core\Menu;
6 * Provides a value object to model an element in a menu link tree.
8 * \Drupal\Core\Menu\MenuLinkTreeElement objects represent a menu link's data.
9 * Objects of this class provide complimentary data: the placement in a tree.
10 * Therefore, we can summarize this split as follows:
11 * - Menu link objects contain all information about an individual menu link,
12 * plus what their parent is. But they don't know where exactly in a menu link
14 * - Instances of this class are complimentary to those objects, they know:
15 * - All additional metadata from {menu_tree}, which contains "materialized"
16 * metadata about a menu link tree, such as whether a link in the tree has
17 * visible children and the depth relative to the root.
18 * - Plus all additional metadata that's adjusted for the current tree query,
19 * such as whether the link is in the active trail, whether the link is
20 * accessible for the current user, and the link's children (which are only
21 * loaded if the link was marked as "expanded" by the query).
23 * @see \Drupal\Core\Menu\MenuTreeStorage::loadTreeData()
25 class MenuLinkTreeElement {
28 * The menu link for this element in a menu link tree.
30 * @var \Drupal\Core\Menu\MenuLinkInterface
35 * The subtree of this element in the menu link tree (this link's children).
37 * (Children of a link are only loaded if a link is marked as "expanded" by
40 * @var \Drupal\Core\Menu\MenuLinkTreeElement[]
45 * The depth of this link relative to the root of the tree.
52 * Whether this link has any children at all.
59 * Whether this link is in the active trail.
63 public $inActiveTrail;
66 * Whether this link is accessible by the current user.
68 * If the value is NULL the access was not determined yet, if an access result
69 * object, it was determined already.
71 * @var \Drupal\Core\Access\AccessResultInterface|null
76 * Additional options for this link.
78 * This is merged (\Drupal\Component\Utility\NestedArray::mergeDeep()) with
79 * \Drupal\Core\Menu\MenuLinkInterface::getOptions(), to allow menu link tree
80 * manipulators to add or override link options.
85 * Constructs a new \Drupal\Core\Menu\MenuLinkTreeElement.
87 * @param \Drupal\Core\Menu\MenuLinkInterface $link
88 * The menu link for this element in the menu link tree.
89 * @param bool $has_children
90 * A flag as to whether this element has children even if they are not
91 * included in the tree (i.e. this may be TRUE even if $subtree is empty).
93 * The depth of this element relative to the tree root.
94 * @param bool $in_active_trail
95 * A flag as to whether this link was included in the list of active trail
96 * IDs used to build the tree.
97 * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $subtree
98 * The children of this element in the menu link tree.
100 public function __construct(MenuLinkInterface $link, $has_children, $depth, $in_active_trail, array $subtree) {
101 // Essential properties.
103 $this->hasChildren = $has_children;
104 $this->depth = $depth;
105 $this->subtree = $subtree;
106 $this->inActiveTrail = $in_active_trail;
110 * Counts all menu links in the current subtree.
113 * The number of menu links in this subtree (one plus the number of menu
114 * links in all descendants).
116 public function count() {
117 $sum = function ($carry, MenuLinkTreeElement $element) {
118 return $carry + $element->count();
120 return 1 + array_reduce($this->subtree, $sum);