Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Menu;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Provides a value object to model an element in a menu link tree.
|
Chris@0
|
7 *
|
Chris@0
|
8 * \Drupal\Core\Menu\MenuLinkTreeElement objects represent a menu link's data.
|
Chris@0
|
9 * Objects of this class provide complimentary data: the placement in a tree.
|
Chris@0
|
10 * Therefore, we can summarize this split as follows:
|
Chris@0
|
11 * - Menu link objects contain all information about an individual menu link,
|
Chris@0
|
12 * plus what their parent is. But they don't know where exactly in a menu link
|
Chris@0
|
13 * tree they live.
|
Chris@0
|
14 * - Instances of this class are complimentary to those objects, they know:
|
Chris@0
|
15 * - All additional metadata from {menu_tree}, which contains "materialized"
|
Chris@0
|
16 * metadata about a menu link tree, such as whether a link in the tree has
|
Chris@0
|
17 * visible children and the depth relative to the root.
|
Chris@0
|
18 * - Plus all additional metadata that's adjusted for the current tree query,
|
Chris@0
|
19 * such as whether the link is in the active trail, whether the link is
|
Chris@0
|
20 * accessible for the current user, and the link's children (which are only
|
Chris@0
|
21 * loaded if the link was marked as "expanded" by the query).
|
Chris@0
|
22 *
|
Chris@0
|
23 * @see \Drupal\Core\Menu\MenuTreeStorage::loadTreeData()
|
Chris@0
|
24 */
|
Chris@0
|
25 class MenuLinkTreeElement {
|
Chris@0
|
26
|
Chris@0
|
27 /**
|
Chris@0
|
28 * The menu link for this element in a menu link tree.
|
Chris@0
|
29 *
|
Chris@0
|
30 * @var \Drupal\Core\Menu\MenuLinkInterface
|
Chris@0
|
31 */
|
Chris@0
|
32 public $link;
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * The subtree of this element in the menu link tree (this link's children).
|
Chris@0
|
36 *
|
Chris@0
|
37 * (Children of a link are only loaded if a link is marked as "expanded" by
|
Chris@0
|
38 * the query.)
|
Chris@0
|
39 *
|
Chris@0
|
40 * @var \Drupal\Core\Menu\MenuLinkTreeElement[]
|
Chris@0
|
41 */
|
Chris@0
|
42 public $subtree;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * The depth of this link relative to the root of the tree.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @var int
|
Chris@0
|
48 */
|
Chris@0
|
49 public $depth;
|
Chris@0
|
50
|
Chris@0
|
51 /**
|
Chris@0
|
52 * Whether this link has any children at all.
|
Chris@0
|
53 *
|
Chris@0
|
54 * @var bool
|
Chris@0
|
55 */
|
Chris@0
|
56 public $hasChildren;
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * Whether this link is in the active trail.
|
Chris@0
|
60 *
|
Chris@0
|
61 * @var bool
|
Chris@0
|
62 */
|
Chris@0
|
63 public $inActiveTrail;
|
Chris@0
|
64
|
Chris@0
|
65 /**
|
Chris@0
|
66 * Whether this link is accessible by the current user.
|
Chris@0
|
67 *
|
Chris@0
|
68 * If the value is NULL the access was not determined yet, if an access result
|
Chris@0
|
69 * object, it was determined already.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @var \Drupal\Core\Access\AccessResultInterface|null
|
Chris@0
|
72 */
|
Chris@0
|
73 public $access;
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * Additional options for this link.
|
Chris@0
|
77 *
|
Chris@0
|
78 * This is merged (\Drupal\Component\Utility\NestedArray::mergeDeep()) with
|
Chris@0
|
79 * \Drupal\Core\Menu\MenuLinkInterface::getOptions(), to allow menu link tree
|
Chris@0
|
80 * manipulators to add or override link options.
|
Chris@0
|
81 */
|
Chris@0
|
82 public $options = [];
|
Chris@0
|
83
|
Chris@0
|
84 /**
|
Chris@0
|
85 * Constructs a new \Drupal\Core\Menu\MenuLinkTreeElement.
|
Chris@0
|
86 *
|
Chris@0
|
87 * @param \Drupal\Core\Menu\MenuLinkInterface $link
|
Chris@0
|
88 * The menu link for this element in the menu link tree.
|
Chris@0
|
89 * @param bool $has_children
|
Chris@0
|
90 * A flag as to whether this element has children even if they are not
|
Chris@0
|
91 * included in the tree (i.e. this may be TRUE even if $subtree is empty).
|
Chris@0
|
92 * @param int $depth
|
Chris@0
|
93 * The depth of this element relative to the tree root.
|
Chris@0
|
94 * @param bool $in_active_trail
|
Chris@0
|
95 * A flag as to whether this link was included in the list of active trail
|
Chris@0
|
96 * IDs used to build the tree.
|
Chris@0
|
97 * @param \Drupal\Core\Menu\MenuLinkTreeElement[] $subtree
|
Chris@0
|
98 * The children of this element in the menu link tree.
|
Chris@0
|
99 */
|
Chris@0
|
100 public function __construct(MenuLinkInterface $link, $has_children, $depth, $in_active_trail, array $subtree) {
|
Chris@0
|
101 // Essential properties.
|
Chris@0
|
102 $this->link = $link;
|
Chris@0
|
103 $this->hasChildren = $has_children;
|
Chris@0
|
104 $this->depth = $depth;
|
Chris@0
|
105 $this->subtree = $subtree;
|
Chris@0
|
106 $this->inActiveTrail = $in_active_trail;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Counts all menu links in the current subtree.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @return int
|
Chris@0
|
113 * The number of menu links in this subtree (one plus the number of menu
|
Chris@0
|
114 * links in all descendants).
|
Chris@0
|
115 */
|
Chris@0
|
116 public function count() {
|
Chris@0
|
117 $sum = function ($carry, MenuLinkTreeElement $element) {
|
Chris@0
|
118 return $carry + $element->count();
|
Chris@0
|
119 };
|
Chris@0
|
120 return 1 + array_reduce($this->subtree, $sum);
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 }
|