class AttributeArray

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

A class that defines a type of Attribute that can be added to as an array.

To use with Attribute, the array must be specified. Correct:

$attributes = new Attribute();
$attributes['class'] = [];
$attributes['class'][] = 'cat';

Incorrect:

$attributes = new Attribute();
$attributes['class'][] = 'cat';

Hierarchy

  • class \Drupal\Core\Template\AttributeValueBase
    • class \Drupal\Core\Template\AttributeArray extends \Drupal\Core\Template\AttributeValueBase implements \Drupal\Core\Template\ArrayAccess, \Drupal\Core\Template\IteratorAggregate

Expanded class hierarchy of AttributeArray

See also

\Drupal\Core\Template\Attribute

1 file declares its use of AttributeArray
AttributeTest.php in core/tests/Drupal/Tests/Core/Template/AttributeTest.php

File

core/lib/Drupal/Core/Template/AttributeArray.php, line 25

Namespace

Drupal\Core\Template
View source
class AttributeArray extends AttributeValueBase implements \ArrayAccess, \IteratorAggregate {
    
    /**
     * Ensures empty array as a result of array_filter will not print '$name=""'.
     *
     * @see \Drupal\Core\Template\AttributeArray::__toString()
     * @see \Drupal\Core\Template\AttributeValueBase::render()
     */
    const RENDER_EMPTY_ATTRIBUTE = FALSE;
    
    /**
     * {@inheritdoc}
     */
    public function offsetGet($offset) {
        return $this->value[$offset];
    }
    
    /**
     * {@inheritdoc}
     */
    public function offsetSet($offset, $value) {
        if (isset($offset)) {
            $this->value[$offset] = $value;
        }
        else {
            $this->value[] = $value;
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function offsetUnset($offset) {
        unset($this->value[$offset]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function offsetExists($offset) {
        return isset($this->value[$offset]);
    }
    
    /**
     * Implements the magic __toString() method.
     */
    public function __toString() {
        // Filter out any empty values before printing.
        $this->value = array_unique(array_filter($this->value));
        return Html::escape(implode(' ', $this->value));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getIterator() {
        return new \ArrayIterator($this->value);
    }
    
    /**
     * Exchange the array for another one.
     *
     * @see ArrayObject::exchangeArray
     *
     * @param array $input
     *   The array input to replace the internal value.
     *
     * @return array
     *   The old array value.
     */
    public function exchangeArray($input) {
        $old = $this->value;
        $this->value = $input;
        return $old;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AttributeArray::exchangeArray public function Exchange the array for another one.
AttributeArray::getIterator public function
AttributeArray::offsetExists public function
AttributeArray::offsetGet public function
AttributeArray::offsetSet public function
AttributeArray::offsetUnset public function
AttributeArray::RENDER_EMPTY_ATTRIBUTE constant Ensures empty array as a result of array_filter will not print '$name=""'. Overrides AttributeValueBase::RENDER_EMPTY_ATTRIBUTE
AttributeArray::__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::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.