comparison core/lib/Drupal/Core/Menu/MenuLinkTreeElement.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Menu;
4
5 /**
6 * Provides a value object to model an element in a menu link tree.
7 *
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
13 * tree they live.
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).
22 *
23 * @see \Drupal\Core\Menu\MenuTreeStorage::loadTreeData()
24 */
25 class MenuLinkTreeElement {
26
27 /**
28 * The menu link for this element in a menu link tree.
29 *
30 * @var \Drupal\Core\Menu\MenuLinkInterface
31 */
32 public $link;
33
34 /**
35 * The subtree of this element in the menu link tree (this link's children).
36 *
37 * (Children of a link are only loaded if a link is marked as "expanded" by
38 * the query.)
39 *
40 * @var \Drupal\Core\Menu\MenuLinkTreeElement[]
41 */
42 public $subtree;
43
44 /**
45 * The depth of this link relative to the root of the tree.
46 *
47 * @var int
48 */
49 public $depth;
50
51 /**
52 * Whether this link has any children at all.
53 *
54 * @var bool
55 */
56 public $hasChildren;
57
58 /**
59 * Whether this link is in the active trail.
60 *
61 * @var bool
62 */
63 public $inActiveTrail;
64
65 /**
66 * Whether this link is accessible by the current user.
67 *
68 * If the value is NULL the access was not determined yet, if an access result
69 * object, it was determined already.
70 *
71 * @var \Drupal\Core\Access\AccessResultInterface|null
72 */
73 public $access;
74
75 /**
76 * Additional options for this link.
77 *
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.
81 */
82 public $options = [];
83
84 /**
85 * Constructs a new \Drupal\Core\Menu\MenuLinkTreeElement.
86 *
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).
92 * @param int $depth
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.
99 */
100 public function __construct(MenuLinkInterface $link, $has_children, $depth, $in_active_trail, array $subtree) {
101 // Essential properties.
102 $this->link = $link;
103 $this->hasChildren = $has_children;
104 $this->depth = $depth;
105 $this->subtree = $subtree;
106 $this->inActiveTrail = $in_active_trail;
107 }
108
109 /**
110 * Counts all menu links in the current subtree.
111 *
112 * @return int
113 * The number of menu links in this subtree (one plus the number of menu
114 * links in all descendants).
115 */
116 public function count() {
117 $sum = function ($carry, MenuLinkTreeElement $element) {
118 return $carry + $element->count();
119 };
120 return 1 + array_reduce($this->subtree, $sum);
121 }
122
123 }