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

21 files declare their use of Attribute
AttributesTest.php in drupal/core/tests/Drupal/Tests/Core/Common/AttributesTest.php
Contains \Drupal\Tests\Core\Common\AttributesTest.
block.admin.inc in drupal/core/modules/block/block.admin.inc
Admin page callbacks for the block module.
book.module in drupal/core/modules/book/book.module
Allows users to create and organize related content in an outline.
ckeditor.admin.inc in drupal/core/modules/ckeditor/ckeditor.admin.inc
Callbacks and theming for the CKEditor toolbar configuration UI.
common.inc in drupal/core/includes/common.inc
Common functions that many Drupal modules will need to reference.

... See full list

2 string references to 'Attribute'
GetSetMethodNormalizerTest::attributeProvider in drupal/core/vendor/symfony/serializer/Symfony/Component/Serializer/Tests/Normalizer/GetSetMethodNormalizerTest.php
tour.schema.yml in drupal/core/modules/tour/config/schema/tour.schema.yml
drupal/core/modules/tour/config/schema/tour.schema.yml

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.