class DrupalAttributes

Class for managing multiple types of attributes commonly found in Drupal.

Hierarchy

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

Expanded class hierarchy of DrupalAttributes

Related topics

Namespace

Drupal\bootstrap\Utility
Source
class DrupalAttributes extends ArrayObject {

  /**
   * Defines the "attributes" storage type constant.
   *
   * @var string
   */
  const ATTRIBUTES = 'attributes';

  /**
   * Defines the "body_attributes" storage type constant.
   *
   * @var string
   */
  const BODY = 'body_attributes';

  /**
   * Defines the "content_attributes" storage type constant.
   *
   * @var string
   */
  const CONTENT = 'content_attributes';

  /**
   * Defines the "description_attributes" storage type constant.
   *
   * @var string
   */
  const DESCRIPTION = 'description_attributes';

  /**
   * Defines the "footer_attributes" storage type constant.
   *
   * @var string
   */
  const FOOTER = 'footer_attributes';

  /**
   * Defines the "header_attributes" storage type constant.
   *
   * @var string
   */
  const HEADER = 'header_attributes';

  /**
   * Defines the "heading_attributes" storage type constant.
   *
   * @var string
   */
  const HEADING = 'heading_attributes';

  /**
   * Defines the "input_group_attributes" storage type constant.
   *
   * @var string
   */
  const INPUT_GROUP = 'input_group_attributes';

  /**
   * Defines the "label_attributes" storage type constant.
   *
   * @var string
   */
  const LABEL = 'label_attributes';

  /**
   * Defines the "navbar_attributes" storage type constant.
   *
   * @var string
   */
  const NAVBAR = 'navbar_attributes';

  /**
   * Defines the "split_button_attributes" storage type constant.
   *
   * @var string
   */
  const SPLIT_BUTTON = 'split_button_attributes';

  /**
   * Defines the "title_attributes" storage type constant.
   *
   * @var string
   */
  const TITLE = 'title_attributes';

  /**
   * Defines the "wrapper_attributes" storage type constant.
   *
   * @var string
   */
  const WRAPPER = 'wrapper_attributes';

  /**
   * Stored attribute instances.
   *
   * @var \Drupal\bootstrap\Utility\Attributes[]
   */
  protected $attributes = [];

  /**
   * A prefix to use for retrieving attribute keys from the array.
   *
   * @var string
   */
  protected $attributePrefix = '';

  /**
   * Add class(es) to an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then add the class(es) to it.
   *
   * @param string|array $class
   *   An individual class or an array of classes to add.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return $this
   *
   * @see \Drupal\bootstrap\Utility\Attributes::addClass()
   */
  public function addClass($class, $type = DrupalAttributes::ATTRIBUTES) {
    $this->getAttributes($type)->addClass($class);
    return $this;
  }

  /**
   * Retrieve a specific attribute from an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then retrieve the attribute from it.
   *
   * @param string $name
   *   The specific attribute to retrieve.
   * @param mixed $default
   *   (optional) The default value to set if the attribute does not exist.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return mixed
   *   A specific attribute value, passed by reference.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getAttribute()
   */
  public function &getAttribute($name, $default = NULL, $type = DrupalAttributes::ATTRIBUTES) {
    return $this->getAttributes($type)->getAttribute($name, $default);
  }

  /**
   * Retrieves a specific attributes object.
   *
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return \Drupal\bootstrap\Utility\Attributes
   *   An attributes object for $type.
   */
  public function getAttributes($type = DrupalAttributes::ATTRIBUTES) {
    if (!isset($this->attributes[$type])) {
      $attributes = &$this->offsetGet($this->attributePrefix . $type, []);
      if ($attributes instanceof Attribute) {
        $attributes = $attributes->toArray();
      }
      $this->attributes[$type] = new Attributes($attributes);
    }
    return $this->attributes[$type];
  }

  /**
   * Retrieves classes from an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then retrieve the set classes from it.
   *
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return array
   *   The classes array, passed by reference.
   *
   * @see \Drupal\bootstrap\Utility\Attributes::getClasses()
   */
  public function &getClasses($type = DrupalAttributes::ATTRIBUTES) {
    return $this->getAttributes($type)->getClasses();
  }

  /**
   * Indicates whether an attributes object has a specific attribute set.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then check there if the attribute exists.
   *
   * @param string $name
   *   The attribute to search for.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return bool
   *   TRUE or FALSE
   *
   * @see \Drupal\bootstrap\Utility\Attributes::hasAttribute()
   */
  public function hasAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
    return $this->getAttributes($type)->hasAttribute($name);
  }

  /**
   * Indicates whether an attributes object has a specific class.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then check there if a class exists in the attributes object.
   *
   * @param string $class
   *   The class to search for.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return bool
   *   TRUE or FALSE
   *
   * @see \Drupal\bootstrap\Utility\Attributes::hasClass()
   */
  public function hasClass($class, $type = DrupalAttributes::ATTRIBUTES) {
    return $this->getAttributes($type)->hasClass($class);
  }

  /**
   * Removes an attribute from an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then remove an attribute from it.
   *
   * @param string|array $name
   *   The name of the attribute to remove.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return $this
   *
   * @see \Drupal\bootstrap\Utility\Attributes::removeAttribute()
   */
  public function removeAttribute($name, $type = DrupalAttributes::ATTRIBUTES) {
    $this->getAttributes($type)->removeAttribute($name);
    return $this;
  }

  /**
   * Removes a class from an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then remove the class(es) from it.
   *
   * @param string|array $class
   *   An individual class or an array of classes to remove.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return $this
   *
   * @see \Drupal\bootstrap\Utility\Attributes::removeClass()
   */
  public function removeClass($class, $type = DrupalAttributes::ATTRIBUTES) {
    $this->getAttributes($type)->removeClass($class);
    return $this;
  }

  /**
   * Replaces a class in an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then replace the class(es) in it.
   *
   * @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.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return $this
   *
   * @see \Drupal\bootstrap\Utility\Attributes::replaceClass()
   */
  public function replaceClass($old, $new, $type = DrupalAttributes::ATTRIBUTES) {
    $this->getAttributes($type)->replaceClass($old, $new);
    return $this;
  }

  /**
   * Sets an attribute on an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then set an attribute on it.
   *
   * @param string $name
   *   The name of the attribute to set.
   * @param mixed $value
   *   The value of the attribute to set.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return $this
   *
   * @see \Drupal\bootstrap\Utility\Attributes::setAttribute()
   */
  public function setAttribute($name, $value, $type = DrupalAttributes::ATTRIBUTES) {
    $this->getAttributes($type)->setAttribute($name, $value);
    return $this;
  }

  /**
   * Sets multiple attributes on an attributes object.
   *
   * This is a wrapper method to retrieve the correct attributes storage object
   * and then merge multiple attributes into it.
   *
   * @param array $values
   *   An associative key/value array of attributes to set.
   * @param string $type
   *   (optional) The type of attributes to use for this method.
   *
   * @return $this
   *
   * @see \Drupal\bootstrap\Utility\Attributes::setAttributes()
   */
  public function setAttributes(array $values, $type = DrupalAttributes::ATTRIBUTES) {
    $this->getAttributes($type)->setAttributes($values);
    return $this;
  }

}

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::__construct public function Array object constructor.
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.
DrupalAttributes::$attributePrefix protected property A prefix to use for retrieving attribute keys from the array.
DrupalAttributes::$attributes protected property Stored attribute instances.
DrupalAttributes::addClass public function Add class(es) to an attributes object.
DrupalAttributes::ATTRIBUTES constant Defines the "attributes" storage type constant.
DrupalAttributes::BODY constant Defines the "body_attributes" storage type constant.
DrupalAttributes::CONTENT constant Defines the "content_attributes" storage type constant.
DrupalAttributes::DESCRIPTION constant Defines the "description_attributes" storage type constant.
DrupalAttributes::FOOTER constant Defines the "footer_attributes" storage type constant.
DrupalAttributes::getAttribute public function Retrieve a specific attribute from an attributes object.
DrupalAttributes::getAttributes public function Retrieves a specific attributes object.
DrupalAttributes::getClasses public function Retrieves classes from an attributes object.
DrupalAttributes::hasAttribute public function Indicates whether an attributes object has a specific attribute set.
DrupalAttributes::hasClass public function Indicates whether an attributes object has a specific class.
DrupalAttributes::HEADER constant Defines the "header_attributes" storage type constant.
DrupalAttributes::HEADING constant Defines the "heading_attributes" storage type constant.
DrupalAttributes::INPUT_GROUP constant Defines the "input_group_attributes" storage type constant.
DrupalAttributes::LABEL constant Defines the "label_attributes" storage type constant.
DrupalAttributes::NAVBAR constant Defines the "navbar_attributes" storage type constant.
DrupalAttributes::removeAttribute public function Removes an attribute from an attributes object.
DrupalAttributes::removeClass public function Removes a class from an attributes object.
DrupalAttributes::replaceClass public function Replaces a class in an attributes object.
DrupalAttributes::setAttribute public function Sets an attribute on an attributes object.
DrupalAttributes::setAttributes public function Sets multiple attributes on an attributes object.
DrupalAttributes::SPLIT_BUTTON constant Defines the "split_button_attributes" storage type constant.
DrupalAttributes::TITLE constant Defines the "title_attributes" storage type constant.
DrupalAttributes::WRAPPER constant Defines the "wrapper_attributes" storage type constant.