class Attribute implements ArrayAccess, IteratorAggregate, MarkupInterface (View source)

Collects, sanitizes, and renders HTML attributes.

To use, optionally pass in an associative array of defined attributes, or add attributes using array syntax. For example:

Properties

protected AttributeValueBase[] $storage

Stores the attribute data.

Methods

__construct(array $attributes = [])

Constructs a \Drupal\Core\Template\Attribute object.

offsetGet($name)

{@inheritdoc}

offsetSet($name, $value)

{@inheritdoc}

createAttributeValue(string $name, mixed $value)

Creates the different types of attribute values.

offsetUnset($name)

{@inheritdoc}

offsetExists($name)

{@inheritdoc}

$this
addClass()

Adds classes or merges them on to array of existing CSS classes.

$this
setAttribute(string $attribute, string|array $value)

Sets values for an attribute key.

bool
hasAttribute(string $name)

Checks if the storage has an attribute with the given name.

$this
removeAttribute()

Removes an attribute from an Attribute object.

$this
removeClass()

Removes argument values from array of existing CSS classes.

getClass()

Gets the class attribute value if set.

bool
hasClass(string $class)

Checks if the class array has the given CSS class.

string
__toString()

Implements the magic __toString() method.

array
toArray()

Returns all storage elements as an array.

__clone()

Implements the magic __clone() method.

getIterator()

{@inheritdoc}

storage()

Returns the whole array.

string
jsonSerialize()

Returns a representation of the object for use in JSON serialization.

$this
merge(Attribute $collection)

Merges an Attribute object into the current storage.

Details

__construct(array $attributes = [])

Constructs a \Drupal\Core\Template\Attribute object.

Parameters

array $attributes

An associative array of key-value pairs to be converted to attributes.

offsetGet($name)

{@inheritdoc}

Parameters

$name

offsetSet($name, $value)

{@inheritdoc}

Parameters

$name
$value

protected AttributeValueBase createAttributeValue(string $name, mixed $value)

Creates the different types of attribute values.

Parameters

string $name

The attribute name.

mixed $value

The attribute value.

Return Value

AttributeValueBase

An AttributeValueBase representation of the attribute's value.

offsetUnset($name)

{@inheritdoc}

Parameters

$name

offsetExists($name)

{@inheritdoc}

Parameters

$name

$this addClass()

Adds classes or merges them on to array of existing CSS classes.

Return Value

$this

$this setAttribute(string $attribute, string|array $value)

Sets values for an attribute key.

Parameters

string $attribute

Name of the attribute.

string|array $value

Value(s) to set for the given attribute key.

Return Value

$this

bool hasAttribute(string $name)

Checks if the storage has an attribute with the given name.

Parameters

string $name

The name of the attribute to check for.

Return Value

bool

Returns TRUE if the attribute exists, or FALSE otherwise.

$this removeAttribute()

Removes an attribute from an Attribute object.

Return Value

$this

$this removeClass()

Removes argument values from array of existing CSS classes.

Return Value

$this

AttributeValueBase getClass()

Gets the class attribute value if set.

This method is implemented to take precedence over hasClass() for Twig 2.0.

Return Value

AttributeValueBase

The class attribute value if set.

See also

twig_get_attribute()

bool hasClass(string $class)

Checks if the class array has the given CSS class.

Parameters

string $class

The CSS class to check for.

Return Value

bool

Returns TRUE if the class exists, or FALSE otherwise.

string __toString()

Implements the magic __toString() method.

Return Value

string

The markup.

array toArray()

Returns all storage elements as an array.

Return Value

array

An associative array of attributes.

__clone()

Implements the magic __clone() method.

getIterator()

{@inheritdoc}

storage()

Returns the whole array.

string jsonSerialize()

Returns a representation of the object for use in JSON serialization.

Return Value

string

The safe string content.

$this merge(Attribute $collection)

Merges an Attribute object into the current storage.

Parameters

Attribute $collection

The Attribute object to merge.

Return Value

$this