AttributeBoolean.php 1.11 KB
Newer Older
1
2
3
4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Template\AttributeBoolean.
6
7
8
 */

namespace Drupal\Core\Template;
9

10
11
12
13
14
15
16
17
18
/**
 * 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.
 * @code
19
 *  $attributes = new Attribute();
20
 *  $attributes['disabled'] = TRUE;
21
 *  echo '<select' . $attributes . '/>';
22
23
 *  // produces <select disabled>;
 *  $attributes['disabled'] = FALSE;
24
 *  echo '<select' . $attributes . '/>';
25
26
27
 *  // produces <select>;
 * @endcode
 *
28
 * @see \Drupal\Core\Template\Attribute
29
30
31
32
33
34
35
36
37
38
39
40
41
42
 */
class AttributeBoolean extends AttributeValueBase {

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

  /**
   * Implements the magic __toString() method.
   */
  public function __toString() {
43
    return $this->value === FALSE ? '' : htmlspecialchars($this->name, ENT_QUOTES, 'UTF-8');
44
45
46
  }

}