Skip to content

Commit 31b613a

Browse files
committed
Update Drupal Attribute classes
1 parent 25affca commit 31b613a

File tree

5 files changed

+22
-33
lines changed

5 files changed

+22
-33
lines changed

src/Drupal/Core/Template/Attribute.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains \Drupal\Core\Template\Attribute.
6-
*/
7-
83
namespace Drupal\Core\Template;
94

105
use Drupal\Component\Render\PlainTextOutput;
11-
use Drupal\Component\Utility\SafeMarkup;
126
use 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
}

src/Drupal/Core/Template/AttributeArray.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains \Drupal\Core\Template\AttributeArray.
6-
*/
7-
83
namespace Drupal\Core\Template;
94

105
use Drupal\Component\Utility\Html;

src/Drupal/Core/Template/AttributeBoolean.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains \Drupal\Core\Template\AttributeBoolean.
6-
*/
7-
83
namespace Drupal\Core\Template;
94

105
use Drupal\Component\Utility\Html;

src/Drupal/Core/Template/AttributeString.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains \Drupal\Core\Template\AttributeString.
6-
*/
7-
83
namespace Drupal\Core\Template;
94

105
use Drupal\Component\Utility\Html;

src/Drupal/Core/Template/AttributeValueBase.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
<?php
22

3-
/**
4-
* @file
5-
* Contains \Drupal\Core\Template\AttributeValueBase.
6-
*/
7-
83
namespace Drupal\Core\Template;
4+
95
use Drupal\Component\Utility\Html;
106

117
/**
@@ -70,6 +66,6 @@ public function value() {
7066
/**
7167
* Implements the magic __toString() method.
7268
*/
73-
abstract function __toString();
69+
abstract public function __toString();
7470

7571
}

0 commit comments

Comments
 (0)