class AttributeBoolean

A class that defines a type of boolean HTML attribute.

Boolean HTML attributes are not attributes with values of TRUE/FALSE. They are attributes that if they exist in the tag, they are TRUE. Examples include selected, disabled, checked, readonly.

To set a boolean attribute on the Attribute class, set it to TRUE.

$attributes = new Attribute(array());
$attributes['disabled'] = TRUE;
echo '<select ' . $attributes . '/>';

// produces <select disabled>;
$attributes['disabled'] = FALSE;
echo '<select ' . $attributes . '/>';

// produces <select>;

Hierarchy

Expanded class hierarchy of AttributeBoolean

See also

Drupal\Core\Template\Attribute

File

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

Namespace

Drupal\Core\Template
View source
class AttributeBoolean extends AttributeValueBase {

  /**
   * Overrides AttributeValueBase::render().
   */
  public function render() {
    return $this
      ->__toString();
  }

  /**
   * Implements the magic __toString() method.
   */
  public function __toString() {
    $this->printed = TRUE;
    return $this->value === FALSE ? '' : check_plain($this->name);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AttributeBoolean::render public function Overrides AttributeValueBase::render(). Overrides AttributeValueBase::render
AttributeBoolean::__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::__construct public function Constructs a \Drupal\Core\Template\AttributeValueBase object.