class Attribute

Same name in this branch

A class that can be used for collecting then rendering HTML attributtes.

To use, one may both pass in an array of already defined attributes and add attributes to it like using array syntax.

$attributes = new Attribute(array(
  'id' => 'socks',
));
$attributes['class'] = array(
  'black-cat',
  'white-cat',
);
$attributes['class'][] = 'black-white-cat';
echo '<cat ' . $attributes . '>';

// Produces <cat id="socks" class="black-cat white-cat black-white-cat">

individual parts of the attribute may be printed first.

$attributes = new Attribute(array(
  'id' => 'socks',
));
$attributes['class'] = array(
  'black-cat',
  'white-cat',
);
$attributes['class'][] = 'black-white-cat';
echo '<cat class="cat ' . $attributes['class'] . '" ' . $attributes . '>';

// Produces <cat class="cat black-cat white-cat black-white-cat" id="socks">

Hierarchy

  • class \Drupal\Core\Template\Attribute implements \Drupal\Core\Template\ArrayAccess, \Drupal\Core\Template\IteratorAggregate

Expanded class hierarchy of Attribute

18 files declare their use of Attribute
AttributesUnitTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Common/AttributesUnitTest.php
Definition of Drupal\system\Tests\Common\AttributesUnitTest.
book.module in drupal/core/modules/book/book.module
Allows users to create and organize related content in an outline.
common.inc in drupal/core/includes/common.inc
Common functions that many Drupal modules will need to reference.
field.module in drupal/core/modules/field/field.module
Attach custom data fields to Drupal entities.
file.module in drupal/core/modules/file/file.module
Defines a "managed_file" Form API field and a "file" field for Field module.

... See full list

File

drupal/core/lib/Drupal/Core/Template/Attribute.php, line 33
Definition of Drupal\Core\Template\Attribute.

Namespace

Drupal\Core\Template
View source
class Attribute implements \ArrayAccess, \IteratorAggregate {

  /**
   * Stores the attribute data.
   *
   * @var array
   */
  protected $storage = array();

  /**
   * Constructs a \Drupal\Core\Template\Attribute object.
   *
   * @param array $attributes
   *   An associative array of key-value pairs to be converted to attributes.
   */
  public function __construct($attributes = array()) {
    foreach ($attributes as $name => $value) {
      $this
        ->offsetSet($name, $value);
    }
  }

  /**
   * Implements ArrayAccess::offsetGet().
   */
  public function offsetGet($name) {
    if (isset($this->storage[$name])) {
      return $this->storage[$name];
    }
  }

  /**
   * Implements ArrayAccess::offsetSet().
   */
  public function offsetSet($name, $value) {
    if (is_array($value)) {
      $value = new AttributeArray($name, $value);
    }
    elseif (is_bool($value)) {
      $value = new AttributeBoolean($name, $value);
    }
    elseif (!is_object($value)) {
      $value = new AttributeString($name, $value);
    }

    // The $name could be NULL.
    if (isset($name)) {
      $this->storage[$name] = $value;
    }
    else {
      $this->storage[] = $value;
    }
  }

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

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

  /**
   * Implements the magic __toString() method.
   */
  public function __toString() {
    $return = '';
    foreach ($this->storage as $name => $value) {
      if (!$value
        ->printed()) {
        $rendered = is_object($value) ? $value
          ->render() : check_plain($name) . ' = "' . check_plain($value) . '"';
        if ($rendered) {
          $return .= " {$rendered}";
        }
      }
    }
    return $return;
  }

  /**
   * Implements the magic __clone() method.
   */
  public function __clone() {
    foreach ($this->storage as $name => $value) {
      if (is_object($value)) {
        $this->storage[$name] = clone $value;
      }
    }
  }

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

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

}

Members

Namesort descending Modifiers Type Description Overrides
Attribute::$storage protected property Stores the attribute data.
Attribute::getIterator public function Implements IteratorAggregate::getIterator().
Attribute::offsetExists public function Implements ArrayAccess::offsetExists().
Attribute::offsetGet public function Implements ArrayAccess::offsetGet().
Attribute::offsetSet public function Implements ArrayAccess::offsetSet().
Attribute::offsetUnset public function Implements ArrayAccess::offsetUnset().
Attribute::value public function Returns the whole array.
Attribute::__clone public function Implements the magic __clone() method.
Attribute::__construct public function Constructs a \Drupal\Core\Template\Attribute object.
Attribute::__toString public function Implements the magic __toString() method.