annotate core/lib/Drupal/Core/Menu/MenuTreeParameters.php @ 17:129ea1e6d783

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