class Attributes

Class to help modify attributes.

Hierarchy

  • class \Drupal\bootstrap\Utility\ArrayObject implements \Countable, \Drupal\Core\Render\AttachmentsInterface, \Drupal\Core\Cache\RefinableCacheableDependencyInterface, \IteratorAggregate, \ArrayAccess, \Serializable

Expanded class hierarchy of Attributes

Related topics

1 file declares its use of Attributes
Menu.php in src/Plugin/Preprocess/Menu.php

Namespace

Drupal\bootstrap\Utility
Source
class Attributes extends ArrayObject {

  /**
   * {@inheritdoc}
   */
  public function __construct(array &$array = []) {
    $this->array = &$array;
  }

  /**
   * Add class(es) to the array.
   *
   * @param string|array $class
   *   An individual class or an array of classes to add.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function addClass($class) {
    // Handle core Attribute based object values.
    // @see https://www.drupal.org/project/bootstrap/issues/3020266
    if ($class instanceof AttributeValueBase) {
      $class = $class->value();
    }
    $classes = &$this->getClasses();
    $classes = array_unique(array_merge($classes, (array) $class));
  }

  /**
   * Retrieve a specific attribute from the array.
   *
   * @param string $name
   *   The specific attribute to retrieve.
   * @param mixed $default
   *   (optional) The default value to set if the attribute does not exist.
   *
   * @return mixed
   *   A specific attribute value, passed by reference.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
   */
  public function &getAttribute($name, $default = NULL) {
    return $this->offsetGet($name, $default);
  }

  /**
   * Retrieves classes from the array.
   *
   * @return array
   *   The classes array, passed by reference.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetGet()
   */
  public function &getClasses() {
    $classes = &$this->offsetGet('class', []);
    $classes = array_unique($classes);
    return $classes;
  }

  /**
   * Indicates whether a specific attribute is set.
   *
   * @param string $name
   *   The attribute to search for.
   *
   * @return bool
   *   TRUE or FALSE
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetExists()
   */
  public function hasAttribute($name) {
    return $this->offsetExists($name);
  }

  /**
   * Indicates whether a class is present in the array.
   *
   * @param string|array $class
   *   The class or array of classes to search for.
   * @param bool $all
   *   Flag determining to check if all classes are present.
   *
   * @return bool
   *   TRUE or FALSE
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function hasClass($class, $all = FALSE) {
    $classes = (array) $class;
    $result = array_intersect($classes, $this->getClasses());
    return $all ? $result && count($classes) === count($result) : !!$result;
  }

  /**
   * Removes an attribute from the array.
   *
   * @param string|array $name
   *   The name of the attribute to remove.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetUnset()
   */
  public function removeAttribute($name) {
    $this->offsetUnset($name);
  }

  /**
   * Removes a class from the attributes array.
   *
   * @param string|array $class
   *   An individual class or an array of classes to remove.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function removeClass($class) {
    $classes = &$this->getClasses();
    $classes = array_values(array_diff($classes, (array) $class));
  }

  /**
   * Replaces a class in the attributes array.
   *
   * @param string $old
   *   The old class to remove.
   * @param string $new
   *   The new class. It will not be added if the $old class does not exist.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function replaceClass($old, $new) {
    $classes = &$this->getClasses();
    $key = array_search($old, $classes);
    if ($key !== FALSE) {
      $classes[$key] = $new;
    }
  }

  /**
   * Sets an attribute on the array.
   *
   * @param string $name
   *   The name of the attribute to set.
   * @param mixed $value
   *   The value of the attribute to set.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::offsetSet()
   */
  public function setAttribute($name, $value) {
    // Handle class attribute differently.
    if ($name === 'class') {
      $this->removeAttribute('class');
      $this->addClass($value);
    }
    else {
      $this->offsetSet($name, $value);
    }
  }

  /**
   * Sets multiple attributes on the array.
   *
   * @param array $values
   *   An associative key/value array of attributes to set.
   *
   * @see \Drupal\bootstrap\Utility\ArrayObject::merge()
   */
  public function setAttributes(array $values) {
    // Handle class attribute differently.
    $classes = isset($values['class']) ? $values['class'] : [];
    unset($values['class']);
    if ($classes) {
      $this->addClass($classes);
    }

    // Merge the reset of the attributes.
    $this->merge($values);
  }

}

Members

Contains filters are case sensitive
Name Modifiers Type Description
ArrayObject::$array protected property The array.
ArrayObject::addAttachments public function
ArrayObject::addCacheableDependency public function
ArrayObject::addCacheContexts public function
ArrayObject::addCacheTags public function
ArrayObject::append public function Appends the value.
ArrayObject::asort public function Sort the entries by value.
ArrayObject::bubbleObject public function Merges an object's cacheable metadata into the variables array.
ArrayObject::bubbleRenderArray public function Merges a render array's cacheable metadata into the variables array.
ArrayObject::count public function Get the number of public properties in the ArrayObject.
ArrayObject::exchangeArray public function Exchange the array for another one.
ArrayObject::getArrayCopy public function Creates a copy of the ArrayObject.
ArrayObject::getAttachments public function
ArrayObject::getCacheContexts public function
ArrayObject::getCacheMaxAge public function
ArrayObject::getCacheTags public function
ArrayObject::getIterator public function Creates a new iterator from an ArrayObject instance.
ArrayObject::ksort public function Sort the entries by key.
ArrayObject::merge public function Merges multiple values into the array.
ArrayObject::mergeCacheMaxAge public function
ArrayObject::natcasesort public function Sort an array using a case insensitive "natural order" algorithm.
ArrayObject::natsort public function Sort entries using a "natural order" algorithm.
ArrayObject::offsetExists public function Returns whether the requested key exists.
ArrayObject::offsetGet public function Returns the value at the specified key.
ArrayObject::offsetSet public function Sets the value at the specified key to value.
ArrayObject::offsetUnset public function Unsets the value at the specified key.
ArrayObject::serialize public function Serialize an ArrayObject.
ArrayObject::setAttachments public function
ArrayObject::uasort public function Sort entries with a user-defined function and maintain key association.
ArrayObject::uksort public function Sort the entries by keys using a user-defined comparison function.
ArrayObject::unserialize public function Unserialize an ArrayObject.
ArrayObject::__get public function Returns the value at the specified key by reference.
ArrayObject::__isset public function Returns whether the requested key exists.
ArrayObject::__set public function Sets the value at the specified key to value.
ArrayObject::__unset public function Unsets the value at the specified key.
Attributes::addClass public function Add class(es) to the array.
Attributes::getAttribute public function Retrieve a specific attribute from the array.
Attributes::getClasses public function Retrieves classes from the array.
Attributes::hasAttribute public function Indicates whether a specific attribute is set.
Attributes::hasClass public function Indicates whether a class is present in the array.
Attributes::removeAttribute public function Removes an attribute from the array.
Attributes::removeClass public function Removes a class from the attributes array.
Attributes::replaceClass public function Replaces a class in the attributes array.
Attributes::setAttribute public function Sets an attribute on the array.
Attributes::setAttributes public function Sets multiple attributes on the array.
Attributes::__construct public function Array object constructor. Overrides ArrayObject::__construct