11<?php
22
3- /**
4- * @file
5- * Contains \Drupal\Core\Template\Attribute.
6- */
7-
83namespace Drupal \Core \Template ;
94
105use Drupal \Component \Render \PlainTextOutput ;
11- use Drupal \Component \Utility \SafeMarkup ;
126use Drupal \Component \Render \MarkupInterface ;
137
148/**
@@ -75,15 +69,15 @@ class Attribute implements \ArrayAccess, \IteratorAggregate, MarkupInterface {
7569 *
7670 * @var \Drupal\Core\Template\AttributeValueBase[]
7771 */
78- protected $ storage = array () ;
72+ protected $ storage = [] ;
7973
8074 /**
8175 * Constructs a \Drupal\Core\Template\Attribute object.
8276 *
8377 * @param array $attributes
8478 * An associative array of key-value pairs to be converted to attributes.
8579 */
86- public function __construct ($ attributes = array () ) {
80+ public function __construct ($ attributes = [] ) {
8781 foreach ($ attributes as $ name => $ value ) {
8882 $ this ->offsetSet ($ name , $ value );
8983 }
@@ -139,7 +133,7 @@ protected function createAttributeValue($name, $value) {
139133 $ value = new AttributeBoolean ($ name , $ value );
140134 }
141135 // As a development aid, we allow the value to be a safe string object.
142- elseif (SafeMarkup:: isSafe ( $ value) ) {
136+ elseif ($ value instanceof MarkupInterface ) {
143137 // Attributes are not supposed to display HTML markup, so we just convert
144138 // the value to plain text.
145139 $ value = PlainTextOutput::renderFromHtml ($ value );
@@ -176,7 +170,7 @@ public function offsetExists($name) {
176170 public function addClass () {
177171 $ args = func_get_args ();
178172 if ($ args ) {
179- $ classes = array () ;
173+ $ classes = [] ;
180174 foreach ($ args as $ arg ) {
181175 // Merge the values passed in from the classes array.
182176 // The argument is cast to an array to support comma separated single
@@ -251,7 +245,7 @@ public function removeClass() {
251245 // With no class attribute, there is no need to remove.
252246 if (isset ($ this ->storage ['class ' ]) && $ this ->storage ['class ' ] instanceof AttributeArray) {
253247 $ args = func_get_args ();
254- $ classes = array () ;
248+ $ classes = [] ;
255249 foreach ($ args as $ arg ) {
256250 // Merge the values passed in from the classes array.
257251 // The argument is cast to an array to support comma separated single
@@ -267,6 +261,20 @@ public function removeClass() {
267261 return $ this ;
268262 }
269263
264+ /**
265+ * Gets the class attribute value if set.
266+ *
267+ * This method is implemented to take precedence over hasClass() for Twig 2.0.
268+ *
269+ * @return \Drupal\Core\Template\AttributeValueBase
270+ * The class attribute value if set.
271+ *
272+ * @see twig_get_attribute()
273+ */
274+ public function getClass () {
275+ return $ this ->offsetGet ('class ' );
276+ }
277+
270278 /**
271279 * Checks if the class array has the given CSS class.
272280 *
@@ -318,7 +326,7 @@ public function toArray() {
318326 /**
319327 * Implements the magic __clone() method.
320328 */
321- public function __clone () {
329+ public function __clone () {
322330 foreach ($ this ->storage as $ name => $ value ) {
323331 $ this ->storage [$ name ] = clone $ value ;
324332 }
0 commit comments