4 * Contains \Drupal\bootstrap\Utility\DrupalAttributes.
7 namespace Drupal\bootstrap\Utility;
9 use Drupal\Core\Template\Attribute;
12 * Class for managing multiple types of attributes commonly found in Drupal.
16 * @see \Drupal\bootstrap\Utility\Attributes
17 * @see \Drupal\bootstrap\Utility\Element
18 * @see \Drupal\bootstrap\Utility\Variables
20 class DrupalAttributes extends ArrayObject {
23 * Defines the "attributes" storage type constant.
27 const ATTRIBUTES = 'attributes';
30 * Defines the "body_attributes" storage type constant.
34 const BODY = 'body_attributes';
37 * Defines the "content_attributes" storage type constant.
41 const CONTENT = 'content_attributes';
44 * Defines the "description_attributes" storage type constant.
48 const DESCRIPTION = 'description_attributes';
51 * Defines the "footer_attributes" storage type constant.
55 const FOOTER = 'footer_attributes';
58 * Defines the "header_attributes" storage type constant.
62 const HEADER = 'header_attributes';
65 * Defines the "heading_attributes" storage type constant.
69 const HEADING = 'heading_attributes';
72 * Defines the "input_group_attributes" storage type constant.
76 const INPUT_GROUP = 'input_group_attributes';
79 * Defines the "label_attributes" storage type constant.
83 const LABEL = 'label_attributes';
86 * Defines the "navbar_attributes" storage type constant.
90 const NAVBAR = 'navbar_attributes';
93 * Defines the "split_button_attributes" storage type constant.
97 const SPLIT_BUTTON = 'split_button_attributes';
100 * Defines the "title_attributes" storage type constant.
104 const TITLE = 'title_attributes';
107 * Defines the "wrapper_attributes" storage type constant.
111 const WRAPPER = 'wrapper_attributes';
114 * Stored attribute instances.
116 * @var \Drupal\bootstrap\Utility\Attributes[]
118 protected $attributes = [];
121 * A prefix to use for retrieving attribute keys from the array.
125 protected $attributePrefix = '';
128 * Add class(es) to an attributes object.
130 * This is a wrapper method to retrieve the correct attributes storage object
131 * and then add the class(es) to it.
133 * @param string|array $class
134 * An individual class or an array of classes to add.
135 * @param string $type
136 * (optional) The type of attributes to use for this method.
140 * @see \Drupal\bootstrap\Utility\Attributes::addClass()
142 public function addClass($class, $type = DrupalAttributes::ATTRIBUTES) {
143 $this->getAttributes($type)->addClass($class);
148 * Retrieve a specific attribute from an attributes object.
150 * This is a wrapper method to retrieve the correct attributes storage object
151 * and then retrieve the attribute from it.
153 * @param string $name
154 * The specific attribute to retrieve.
155 * @param mixed $default
156 * (optional) The default value to set if the attribute does not exist.
157 * @param string $type
158 * (optional) The type of attributes to use for this method.
161 * A specific attribute value, passed by reference.
163 * @see \Drupal\bootstrap\Utility\Attributes::getAttribute()
165 public function &getAttribute($name, $default = NULL, $type = DrupalAttributes::ATTRIBUTES) {
166 return $this->getAttributes($type)->getAttribute($name, $default);
170 * Retrieves a specific attributes object.
172 * @param string $type
173 * (optional) The type of attributes to use for this method.
175 * @return \Drupal\bootstrap\Utility\Attributes
176 * An attributes object for $type.
178 public function getAttributes($type = DrupalAttributes::ATTRIBUTES) {
179 if (!isset($this->attributes[$type])) {
180 $attributes = &$this->offsetGet($this->attributePrefix . $type, []);
181 if ($attributes instanceof Attribute) {
182 $attributes = $attributes->toArray();
184 $this->attributes[$type] = new Attributes($attributes);
186 return $this->attributes[$type];
190 * Retrieves classes from an attributes object.
192 * This is a wrapper method to retrieve the correct attributes storage object
193 * and then retrieve the set classes from it.
195 * @param string $type
196 * (optional) The type of attributes to use for this method.
199 * The classes array, passed by reference.
201 * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
203 public function &getClasses($type = DrupalAttributes::ATTRIBUTES) {
204 return $this->getAttributes($type)->getClasses();
208 * Indicates whether an attributes object has a specific attribute set.
210 * This is a wrapper method to retrieve the correct attributes storage object
211 * and then check there if the attribute exists.
213 * @param string $name
214 * The attribute to search for.
215 * @param string $type
216 * (optional) The type of attributes to use for this method.
221 * @see \Drupal\bootstrap\Utility\Attributes::hasAttribute()
223 public function hasAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
224 return $this->getAttributes($type)->hasAttribute($name);
228 * Indicates whether an attributes object has a specific class.
230 * This is a wrapper method to retrieve the correct attributes storage object
231 * and then check there if a class exists in the attributes object.
233 * @param string $class
234 * The class to search for.
235 * @param string $type
236 * (optional) The type of attributes to use for this method.
241 * @see \Drupal\bootstrap\Utility\Attributes::hasClass()
243 public function hasClass($class, $type = DrupalAttributes::ATTRIBUTES) {
244 return $this->getAttributes($type)->hasClass($class);
248 * Removes an attribute from an attributes object.
250 * This is a wrapper method to retrieve the correct attributes storage object
251 * and then remove an attribute from it.
253 * @param string|array $name
254 * The name of the attribute to remove.
255 * @param string $type
256 * (optional) The type of attributes to use for this method.
260 * @see \Drupal\bootstrap\Utility\Attributes::removeAttribute()
262 public function removeAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
263 $this->getAttributes($type)->removeAttribute($name);
268 * Removes a class from an attributes object.
270 * This is a wrapper method to retrieve the correct attributes storage object
271 * and then remove the class(es) from it.
273 * @param string|array $class
274 * An individual class or an array of classes to remove.
275 * @param string $type
276 * (optional) The type of attributes to use for this method.
280 * @see \Drupal\bootstrap\Utility\Attributes::removeClass()
282 public function removeClass($class, $type = DrupalAttributes::ATTRIBUTES) {
283 $this->getAttributes($type)->removeClass($class);
288 * Replaces a class in an attributes object.
290 * This is a wrapper method to retrieve the correct attributes storage object
291 * and then replace the class(es) in it.
294 * The old class to remove.
296 * The new class. It will not be added if the $old class does not exist.
297 * @param string $type
298 * (optional) The type of attributes to use for this method.
302 * @see \Drupal\bootstrap\Utility\Attributes::replaceClass()
304 public function replaceClass($old, $new, $type = DrupalAttributes::ATTRIBUTES) {
305 $this->getAttributes($type)->replaceClass($old, $new);
310 * Sets an attribute on an attributes object.
312 * This is a wrapper method to retrieve the correct attributes storage object
313 * and then set an attribute on it.
315 * @param string $name
316 * The name of the attribute to set.
317 * @param mixed $value
318 * The value of the attribute to set.
319 * @param string $type
320 * (optional) The type of attributes to use for this method.
324 * @see \Drupal\bootstrap\Utility\Attributes::setAttribute()
326 public function setAttribute($name, $value, $type = DrupalAttributes::ATTRIBUTES) {
327 $this->getAttributes($type)->setAttribute($name, $value);
332 * Sets multiple attributes on an attributes object.
334 * This is a wrapper method to retrieve the correct attributes storage object
335 * and then merge multiple attributes into it.
337 * @param array $values
338 * An associative key/value array of attributes to set.
339 * @param string $type
340 * (optional) The type of attributes to use for this method.
344 * @see \Drupal\bootstrap\Utility\Attributes::setAttributes()
346 public function setAttributes(array $values, $type = DrupalAttributes::ATTRIBUTES) {
347 $this->getAttributes($type)->setAttributes($values);