AttributeBoolean.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Drupal\Core\Template;
  3. use Drupal\Component\Utility\Html;
  4. /**
  5. * A class that defines a type of boolean HTML attribute.
  6. *
  7. * Boolean HTML attributes are not attributes with values of TRUE/FALSE.
  8. * They are attributes that if they exist in the tag, they are TRUE.
  9. * Examples include selected, disabled, checked, readonly.
  10. *
  11. * To set a boolean attribute on the Attribute class, set it to TRUE.
  12. * @code
  13. * $attributes = new Attribute();
  14. * $attributes['disabled'] = TRUE;
  15. * echo '<select' . $attributes . '/>';
  16. * // produces <select disabled>;
  17. * $attributes['disabled'] = FALSE;
  18. * echo '<select' . $attributes . '/>';
  19. * // produces <select>;
  20. * @endcode
  21. *
  22. * @see \Drupal\Core\Template\Attribute
  23. */
  24. class AttributeBoolean extends AttributeValueBase {
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function render() {
  29. return $this->__toString();
  30. }
  31. /**
  32. * Implements the magic __toString() method.
  33. */
  34. public function __toString() {
  35. return $this->value === FALSE ? '' : Html::escape($this->name);
  36. }
  37. }