1
1
<?php
2
2
3
- /**
4
- * @file
5
- * Contains \Drupal\Core\Template\Attribute.
6
- */
7
-
8
3
namespace Drupal \Core \Template ;
9
4
10
5
use Drupal \Component \Render \PlainTextOutput ;
11
- use Drupal \Component \Utility \SafeMarkup ;
12
6
use Drupal \Component \Render \MarkupInterface ;
13
7
14
8
/**
@@ -75,15 +69,15 @@ class Attribute implements \ArrayAccess, \IteratorAggregate, MarkupInterface {
75
69
*
76
70
* @var \Drupal\Core\Template\AttributeValueBase[]
77
71
*/
78
- protected $ storage = array () ;
72
+ protected $ storage = [] ;
79
73
80
74
/**
81
75
* Constructs a \Drupal\Core\Template\Attribute object.
82
76
*
83
77
* @param array $attributes
84
78
* An associative array of key-value pairs to be converted to attributes.
85
79
*/
86
- public function __construct ($ attributes = array () ) {
80
+ public function __construct ($ attributes = [] ) {
87
81
foreach ($ attributes as $ name => $ value ) {
88
82
$ this ->offsetSet ($ name , $ value );
89
83
}
@@ -139,7 +133,7 @@ protected function createAttributeValue($name, $value) {
139
133
$ value = new AttributeBoolean ($ name , $ value );
140
134
}
141
135
// As a development aid, we allow the value to be a safe string object.
142
- elseif (SafeMarkup:: isSafe ( $ value) ) {
136
+ elseif ($ value instanceof MarkupInterface ) {
143
137
// Attributes are not supposed to display HTML markup, so we just convert
144
138
// the value to plain text.
145
139
$ value = PlainTextOutput::renderFromHtml ($ value );
@@ -176,7 +170,7 @@ public function offsetExists($name) {
176
170
public function addClass () {
177
171
$ args = func_get_args ();
178
172
if ($ args ) {
179
- $ classes = array () ;
173
+ $ classes = [] ;
180
174
foreach ($ args as $ arg ) {
181
175
// Merge the values passed in from the classes array.
182
176
// The argument is cast to an array to support comma separated single
@@ -251,7 +245,7 @@ public function removeClass() {
251
245
// With no class attribute, there is no need to remove.
252
246
if (isset ($ this ->storage ['class ' ]) && $ this ->storage ['class ' ] instanceof AttributeArray) {
253
247
$ args = func_get_args ();
254
- $ classes = array () ;
248
+ $ classes = [] ;
255
249
foreach ($ args as $ arg ) {
256
250
// Merge the values passed in from the classes array.
257
251
// The argument is cast to an array to support comma separated single
@@ -267,6 +261,20 @@ public function removeClass() {
267
261
return $ this ;
268
262
}
269
263
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
+
270
278
/**
271
279
* Checks if the class array has the given CSS class.
272
280
*
@@ -318,7 +326,7 @@ public function toArray() {
318
326
/**
319
327
* Implements the magic __clone() method.
320
328
*/
321
- public function __clone () {
329
+ public function __clone () {
322
330
foreach ($ this ->storage as $ name => $ value ) {
323
331
$ this ->storage [$ name ] = clone $ value ;
324
332
}
0 commit comments