file DrupalAttributes.php

Namespace

Drupal\bootstrap\Utility
  1. <?php
  2. namespace Drupal\bootstrap\Utility;
  3. use Drupal\Core\Template\Attribute;
  4. /**
  5. * Class for managing multiple types of attributes commonly found in Drupal.
  6. *
  7. * @ingroup utility
  8. *
  9. * @see \Drupal\bootstrap\Utility\Attributes
  10. * @see \Drupal\bootstrap\Utility\Element
  11. * @see \Drupal\bootstrap\Utility\Variables
  12. */
  13. class DrupalAttributes extends ArrayObject {
  14. /**
  15. * Defines the "attributes" storage type constant.
  16. *
  17. * @var string
  18. */
  19. const ATTRIBUTES = 'attributes';
  20. /**
  21. * Defines the "body_attributes" storage type constant.
  22. *
  23. * @var string
  24. */
  25. const BODY = 'body_attributes';
  26. /**
  27. * Defines the "content_attributes" storage type constant.
  28. *
  29. * @var string
  30. */
  31. const CONTENT = 'content_attributes';
  32. /**
  33. * Defines the "description_attributes" storage type constant.
  34. *
  35. * @var string
  36. */
  37. const DESCRIPTION = 'description_attributes';
  38. /**
  39. * Defines the "footer_attributes" storage type constant.
  40. *
  41. * @var string
  42. */
  43. const FOOTER = 'footer_attributes';
  44. /**
  45. * Defines the "header_attributes" storage type constant.
  46. *
  47. * @var string
  48. */
  49. const HEADER = 'header_attributes';
  50. /**
  51. * Defines the "heading_attributes" storage type constant.
  52. *
  53. * @var string
  54. */
  55. const HEADING = 'heading_attributes';
  56. /**
  57. * Defines the "input_group_attributes" storage type constant.
  58. *
  59. * @var string
  60. */
  61. const INPUT_GROUP = 'input_group_attributes';
  62. /**
  63. * Defines the "label_attributes" storage type constant.
  64. *
  65. * @var string
  66. */
  67. const LABEL = 'label_attributes';
  68. /**
  69. * Defines the "navbar_attributes" storage type constant.
  70. *
  71. * @var string
  72. */
  73. const NAVBAR = 'navbar_attributes';
  74. /**
  75. * Defines the "split_button_attributes" storage type constant.
  76. *
  77. * @var string
  78. */
  79. const SPLIT_BUTTON = 'split_button_attributes';
  80. /**
  81. * Defines the "title_attributes" storage type constant.
  82. *
  83. * @var string
  84. */
  85. const TITLE = 'title_attributes';
  86. /**
  87. * Defines the "wrapper_attributes" storage type constant.
  88. *
  89. * @var string
  90. */
  91. const WRAPPER = 'wrapper_attributes';
  92. /**
  93. * Stored attribute instances.
  94. *
  95. * @var \Drupal\bootstrap\Utility\Attributes[]
  96. */
  97. protected $attributes = [];
  98. /**
  99. * A prefix to use for retrieving attribute keys from the array.
  100. *
  101. * @var string
  102. */
  103. protected $attributePrefix = '';
  104. /**
  105. * Add class(es) to an attributes object.
  106. *
  107. * This is a wrapper method to retrieve the correct attributes storage object
  108. * and then add the class(es) to it.
  109. *
  110. * @param string|array $class
  111. * An individual class or an array of classes to add.
  112. * @param string $type
  113. * (optional) The type of attributes to use for this method.
  114. *
  115. * @return $this
  116. *
  117. * @see \Drupal\bootstrap\Utility\Attributes::addClass()
  118. */
  119. public function addClass($class, $type = DrupalAttributes::ATTRIBUTES) {
  120. $this->getAttributes($type)->addClass($class);
  121. return $this;
  122. }
  123. /**
  124. * Retrieve a specific attribute from an attributes object.
  125. *
  126. * This is a wrapper method to retrieve the correct attributes storage object
  127. * and then retrieve the attribute from it.
  128. *
  129. * @param string $name
  130. * The specific attribute to retrieve.
  131. * @param mixed $default
  132. * (optional) The default value to set if the attribute does not exist.
  133. * @param string $type
  134. * (optional) The type of attributes to use for this method.
  135. *
  136. * @return mixed
  137. * A specific attribute value, passed by reference.
  138. *
  139. * @see \Drupal\bootstrap\Utility\Attributes::getAttribute()
  140. */
  141. public function &getAttribute($name, $default = NULL, $type = DrupalAttributes::ATTRIBUTES) {
  142. return $this->getAttributes($type)->getAttribute($name, $default);
  143. }
  144. /**
  145. * Retrieves a specific attributes object.
  146. *
  147. * @param string $type
  148. * (optional) The type of attributes to use for this method.
  149. *
  150. * @return \Drupal\bootstrap\Utility\Attributes
  151. * An attributes object for $type.
  152. */
  153. public function getAttributes($type = DrupalAttributes::ATTRIBUTES) {
  154. if (!isset($this->attributes[$type])) {
  155. $attributes = &$this->offsetGet($this->attributePrefix . $type, []);
  156. if ($attributes instanceof Attribute) {
  157. $attributes = $attributes->toArray();
  158. }
  159. $this->attributes[$type] = new Attributes($attributes);
  160. }
  161. return $this->attributes[$type];
  162. }
  163. /**
  164. * Retrieves classes from an attributes object.
  165. *
  166. * This is a wrapper method to retrieve the correct attributes storage object
  167. * and then retrieve the set classes from it.
  168. *
  169. * @param string $type
  170. * (optional) The type of attributes to use for this method.
  171. *
  172. * @return array
  173. * The classes array, passed by reference.
  174. *
  175. * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
  176. */
  177. public function &getClasses($type = DrupalAttributes::ATTRIBUTES) {
  178. return $this->getAttributes($type)->getClasses();
  179. }
  180. /**
  181. * Indicates whether an attributes object has a specific attribute set.
  182. *
  183. * This is a wrapper method to retrieve the correct attributes storage object
  184. * and then check there if the attribute exists.
  185. *
  186. * @param string $name
  187. * The attribute to search for.
  188. * @param string $type
  189. * (optional) The type of attributes to use for this method.
  190. *
  191. * @return bool
  192. * TRUE or FALSE
  193. *
  194. * @see \Drupal\bootstrap\Utility\Attributes::hasAttribute()
  195. */
  196. public function hasAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
  197. return $this->getAttributes($type)->hasAttribute($name);
  198. }
  199. /**
  200. * Indicates whether an attributes object has a specific class.
  201. *
  202. * This is a wrapper method to retrieve the correct attributes storage object
  203. * and then check there if a class exists in the attributes object.
  204. *
  205. * @param string $class
  206. * The class to search for.
  207. * @param string $type
  208. * (optional) The type of attributes to use for this method.
  209. *
  210. * @return bool
  211. * TRUE or FALSE
  212. *
  213. * @see \Drupal\bootstrap\Utility\Attributes::hasClass()
  214. */
  215. public function hasClass($class, $type = DrupalAttributes::ATTRIBUTES) {
  216. return $this->getAttributes($type)->hasClass($class);
  217. }
  218. /**
  219. * Removes an attribute from an attributes object.
  220. *
  221. * This is a wrapper method to retrieve the correct attributes storage object
  222. * and then remove an attribute from it.
  223. *
  224. * @param string|array $name
  225. * The name of the attribute to remove.
  226. * @param string $type
  227. * (optional) The type of attributes to use for this method.
  228. *
  229. * @return $this
  230. *
  231. * @see \Drupal\bootstrap\Utility\Attributes::removeAttribute()
  232. */
  233. public function removeAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
  234. $this->getAttributes($type)->removeAttribute($name);
  235. return $this;
  236. }
  237. /**
  238. * Removes a class from an attributes object.
  239. *
  240. * This is a wrapper method to retrieve the correct attributes storage object
  241. * and then remove the class(es) from it.
  242. *
  243. * @param string|array $class
  244. * An individual class or an array of classes to remove.
  245. * @param string $type
  246. * (optional) The type of attributes to use for this method.
  247. *
  248. * @return $this
  249. *
  250. * @see \Drupal\bootstrap\Utility\Attributes::removeClass()
  251. */
  252. public function removeClass($class, $type = DrupalAttributes::ATTRIBUTES) {
  253. $this->getAttributes($type)->removeClass($class);
  254. return $this;
  255. }
  256. /**
  257. * Replaces a class in an attributes object.
  258. *
  259. * This is a wrapper method to retrieve the correct attributes storage object
  260. * and then replace the class(es) in it.
  261. *
  262. * @param string $old
  263. * The old class to remove.
  264. * @param string $new
  265. * The new class. It will not be added if the $old class does not exist.
  266. * @param string $type
  267. * (optional) The type of attributes to use for this method.
  268. *
  269. * @return $this
  270. *
  271. * @see \Drupal\bootstrap\Utility\Attributes::replaceClass()
  272. */
  273. public function replaceClass($old, $new, $type = DrupalAttributes::ATTRIBUTES) {
  274. $this->getAttributes($type)->replaceClass($old, $new);
  275. return $this;
  276. }
  277. /**
  278. * Sets an attribute on an attributes object.
  279. *
  280. * This is a wrapper method to retrieve the correct attributes storage object
  281. * and then set an attribute on it.
  282. *
  283. * @param string $name
  284. * The name of the attribute to set.
  285. * @param mixed $value
  286. * The value of the attribute to set.
  287. * @param string $type
  288. * (optional) The type of attributes to use for this method.
  289. *
  290. * @return $this
  291. *
  292. * @see \Drupal\bootstrap\Utility\Attributes::setAttribute()
  293. */
  294. public function setAttribute($name, $value, $type = DrupalAttributes::ATTRIBUTES) {
  295. $this->getAttributes($type)->setAttribute($name, $value);
  296. return $this;
  297. }
  298. /**
  299. * Sets multiple attributes on an attributes object.
  300. *
  301. * This is a wrapper method to retrieve the correct attributes storage object
  302. * and then merge multiple attributes into it.
  303. *
  304. * @param array $values
  305. * An associative key/value array of attributes to set.
  306. * @param string $type
  307. * (optional) The type of attributes to use for this method.
  308. *
  309. * @return $this
  310. *
  311. * @see \Drupal\bootstrap\Utility\Attributes::setAttributes()
  312. */
  313. public function setAttributes(array $values, $type = DrupalAttributes::ATTRIBUTES) {
  314. $this->getAttributes($type)->setAttributes($values);
  315. return $this;
  316. }
  317. }

Classes

Name Description
DrupalAttributes Class for managing multiple types of attributes commonly found in Drupal.