MenuTreeParameters.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace Drupal\Core\Menu;
  3. /**
  4. * Provides a value object to model menu tree parameters.
  5. *
  6. * Menu tree parameters are used to determine the set of definitions to be
  7. * loaded from \Drupal\Core\Menu\MenuTreeStorageInterface. Hence they determine
  8. * the shape and content of the tree:
  9. * - Which parent IDs should be used to restrict the tree. Only links with
  10. * a parent in the list will be included.
  11. * - Which menu links are omitted, depending on the minimum and maximum depth.
  12. */
  13. class MenuTreeParameters {
  14. /**
  15. * A menu link plugin ID that should be used as the root.
  16. *
  17. * By default the root ID of empty string '' is used. However, when only the
  18. * descendants (subtree) of a certain menu link are needed, a custom root can
  19. * be specified.
  20. *
  21. * @var string
  22. */
  23. public $root = '';
  24. /**
  25. * The minimum depth of menu links in the resulting tree relative to the root.
  26. *
  27. * Defaults to 1, which is the default to build a whole tree for a menu
  28. * (excluding the root).
  29. *
  30. * @var int|null
  31. */
  32. public $minDepth = NULL;
  33. /**
  34. * The maximum depth of menu links in the resulting tree relative to the root.
  35. *
  36. * @var int|null
  37. */
  38. public $maxDepth = NULL;
  39. /**
  40. * An array of parent link IDs.
  41. *
  42. * This restricts the tree to only menu links that are at the top level or
  43. * have a parent ID in this list. If empty, the whole menu tree is built.
  44. *
  45. * @var string[]
  46. */
  47. public $expandedParents = [];
  48. /**
  49. * The IDs from the currently active menu link to the root of the whole tree.
  50. *
  51. * This is an array of menu link plugin IDs, representing the trail from the
  52. * currently active menu link to the ("real") root of that menu link's menu.
  53. * This does not affect the way the tree is built. It is only used to set the
  54. * value of the inActiveTrail property for each tree element.
  55. *
  56. * @var string[]
  57. */
  58. public $activeTrail = [];
  59. /**
  60. * The conditions used to restrict which links are loaded.
  61. *
  62. * An associative array of custom query condition key/value pairs.
  63. *
  64. * @var array
  65. */
  66. public $conditions = [];
  67. /**
  68. * Sets a root for menu tree loading.
  69. *
  70. * @param string $root
  71. * A menu link plugin ID, or empty string '' to use the root of the whole
  72. * tree.
  73. *
  74. * @return $this
  75. *
  76. * @codeCoverageIgnore
  77. */
  78. public function setRoot($root) {
  79. $this->root = (string) $root;
  80. return $this;
  81. }
  82. /**
  83. * Sets a minimum depth for menu tree loading.
  84. *
  85. * @param int $min_depth
  86. * The (root-relative) minimum depth to apply.
  87. *
  88. * @return $this
  89. */
  90. public function setMinDepth($min_depth) {
  91. $this->minDepth = max(1, $min_depth);
  92. return $this;
  93. }
  94. /**
  95. * Sets a maximum depth for menu tree loading.
  96. *
  97. * @param int $max_depth
  98. * The (root-relative) maximum depth to apply.
  99. *
  100. * @return $this
  101. *
  102. * @codeCoverageIgnore
  103. */
  104. public function setMaxDepth($max_depth) {
  105. $this->maxDepth = $max_depth;
  106. return $this;
  107. }
  108. /**
  109. * Adds parent menu links IDs to restrict the tree.
  110. *
  111. * @param string[] $parents
  112. * An array containing parent IDs. If supplied, the tree is limited to
  113. * links that have these parents.
  114. *
  115. * @return $this
  116. */
  117. public function addExpandedParents(array $parents) {
  118. $this->expandedParents = array_merge($this->expandedParents, $parents);
  119. $this->expandedParents = array_unique($this->expandedParents);
  120. return $this;
  121. }
  122. /**
  123. * Sets the active trail IDs used to set the inActiveTrail property.
  124. *
  125. * @param string[] $active_trail
  126. * An array containing the active trail: a list of menu link plugin IDs.
  127. *
  128. * @return $this
  129. *
  130. * @see \Drupal\Core\Menu\MenuActiveTrail::getActiveTrailIds()
  131. *
  132. * @codeCoverageIgnore
  133. */
  134. public function setActiveTrail(array $active_trail) {
  135. $this->activeTrail = $active_trail;
  136. return $this;
  137. }
  138. /**
  139. * Adds a custom query condition.
  140. *
  141. * @param string $definition_field
  142. * Only conditions that are testing menu link definition fields are allowed.
  143. * @param mixed $value
  144. * The value to test the link definition field against. In most cases, this
  145. * is a scalar. For more complex options, it is an array. The meaning of
  146. * each element in the array is dependent on the $operator.
  147. * @param string|null $operator
  148. * (optional) The comparison operator, such as =, <, or >=. It also accepts
  149. * more complex options such as IN, LIKE, or BETWEEN. If NULL, defaults to
  150. * the = operator.
  151. *
  152. * @return $this
  153. */
  154. public function addCondition($definition_field, $value, $operator = NULL) {
  155. if (!isset($operator)) {
  156. $this->conditions[$definition_field] = $value;
  157. }
  158. else {
  159. $this->conditions[$definition_field] = [$value, $operator];
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Excludes links that are not enabled.
  165. *
  166. * @return $this
  167. */
  168. public function onlyEnabledLinks() {
  169. $this->addCondition('enabled', 1);
  170. return $this;
  171. }
  172. /**
  173. * Ensures only the top level of the tree is loaded.
  174. *
  175. * @return $this
  176. */
  177. public function setTopLevelOnly() {
  178. $this->setMaxDepth(1);
  179. return $this;
  180. }
  181. /**
  182. * Excludes the root menu link from the tree.
  183. *
  184. * Note that this is only necessary when you specified a custom root, because
  185. * the normal root ID is the empty string, '', which does not correspond to an
  186. * actual menu link. Hence when loading a menu link tree without specifying a
  187. * custom root the tree will start at the children even if this method has not
  188. * been called.
  189. *
  190. * @return $this
  191. */
  192. public function excludeRoot() {
  193. $this->setMinDepth(1);
  194. return $this;
  195. }
  196. }