class AttributeString

Same name in other branches
  1. 9 core/lib/Drupal/Core/Template/AttributeString.php \Drupal\Core\Template\AttributeString
  2. 8.9.x core/lib/Drupal/Core/Template/AttributeString.php \Drupal\Core\Template\AttributeString
  3. 11.x core/lib/Drupal/Core/Template/AttributeString.php \Drupal\Core\Template\AttributeString

A class that represents most standard HTML attributes.

To use with the Attribute class, set the key to be the attribute name and the value the attribute value.

$attributes = new Attribute([]);
$attributes['id'] = 'socks';
$attributes['style'] = 'background-color:white';
echo '<cat ' . $attributes . '>';
// Produces: <cat id="socks" style="background-color:white">.

Hierarchy

Expanded class hierarchy of AttributeString

See also

\Drupal\Core\Template\Attribute

2 files declare their use of AttributeString
AttributeTest.php in core/tests/Drupal/Tests/Core/Template/AttributeTest.php
PreprocessPagerTest.php in core/modules/system/tests/src/Unit/Pager/PreprocessPagerTest.php

File

core/lib/Drupal/Core/Template/AttributeString.php, line 22

Namespace

Drupal\Core\Template
View source
class AttributeString extends AttributeValueBase {
    
    /**
     * Implements the magic __toString() method.
     */
    public function __toString() {
        return Html::escape((string) $this->value);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AttributeString::__toString public function Implements the magic __toString() method. Overrides AttributeValueBase::__toString
AttributeValueBase::$name protected property The name of the value.
AttributeValueBase::$value protected property The value itself.
AttributeValueBase::render public function Returns a string representation of the attribute. 1
AttributeValueBase::RENDER_EMPTY_ATTRIBUTE constant Renders &#039;$name=&quot;&quot;&#039; if $value is an empty string. 1
AttributeValueBase::value public function Returns the raw value.
AttributeValueBase::__construct public function Constructs a \Drupal\Core\Template\AttributeValueBase object.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.