class 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(array());
$attributes['class'] = array();
$attributes['class'][] = 'cat';

Incorrect:

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

Hierarchy

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

Expanded class hierarchy of AttributeArray

See also

Drupal\Core\Template\Attribute

File

drupal/core/lib/Drupal/Core/Template/AttributeArray.php, line 30
Definition of Drupal\Core\Template\AttributeArray.

Namespace

Drupal\Core\Template
View source
class AttributeArray extends AttributeValueBase implements \ArrayAccess, \IteratorAggregate {

  /**
   * Implements ArrayAccess::offsetGet().
   */
  public function offsetGet($offset) {
    return $this->value[$offset];
  }

  /**
   * Implements ArrayAccess::offsetSet().
   */
  public function offsetSet($offset, $value) {
    if (isset($offset)) {
      $this->value[$offset] = $value;
    }
    else {
      $this->value[] = $value;
    }
  }

  /**
   * Implements ArrayAccess::offsetUnset().
   */
  public function offsetUnset($offset) {
    unset($this->value[$offset]);
  }

  /**
   * Implements ArrayAccess::offsetExists().
   */
  public function offsetExists($offset) {
    return isset($this->value[$offset]);
  }

  /**
   * Implements the magic __toString() method.
   */
  public function __toString() {
    $this->printed = TRUE;
    return implode(' ', array_map(array(
      'Drupal\\Component\\Utility\\String',
      'checkPlain',
    ), $this->value));
  }

  /**
   * Implements IteratorAggregate::getIterator().
   */
  public function getIterator() {
    return new \ArrayIterator($this->value);
  }

  /**
   * Returns the whole array.
   */
  public function value() {
    return $this->value;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AttributeArray::getIterator public function Implements IteratorAggregate::getIterator().
AttributeArray::offsetExists public function Implements ArrayAccess::offsetExists().
AttributeArray::offsetGet public function Implements ArrayAccess::offsetGet().
AttributeArray::offsetSet public function Implements ArrayAccess::offsetSet().
AttributeArray::offsetUnset public function Implements ArrayAccess::offsetUnset().
AttributeArray::value public function Returns the whole array.
AttributeArray::__toString public function Implements the magic __toString() method. Overrides AttributeValueBase::__toString
AttributeValueBase::$name protected property The name of the value.
AttributeValueBase::$printed protected property Whether this attribute hsa been printed already.
AttributeValueBase::$value protected property The value itself.
AttributeValueBase::printed public function Whether this attribute hsa been printed already.
AttributeValueBase::render public function Returns a string representation of the attribute. 1
AttributeValueBase::__construct public function Constructs a \Drupal\Core\Template\AttributeValueBase object.