MenuLinkTreeElement.php 3.8 KB

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