3 namespace Drupal\Core\Template;
5 use Drupal\Component\Utility\Html;
8 * Defines the base class for an attribute type.
10 * @see \Drupal\Core\Template\Attribute
12 abstract class AttributeValueBase {
15 * Renders '$name=""' if $value is an empty string.
17 * @see \Drupal\Core\Template\AttributeValueBase::render()
19 const RENDER_EMPTY_ATTRIBUTE = TRUE;
29 * The name of the value.
36 * Constructs a \Drupal\Core\Template\AttributeValueBase object.
38 public function __construct($name, $value) {
40 $this->value = $value;
44 * Returns a string representation of the attribute.
46 * While __toString only returns the value in a string form, render()
47 * contains the name of the attribute as well.
50 * The string representation of the attribute.
52 public function render() {
53 $value = (string) $this;
54 if (isset($this->value) && static::RENDER_EMPTY_ATTRIBUTE || !empty($value)) {
55 return Html::escape($this->name) . '="' . $value . '"';
60 * Returns the raw value.
62 public function value() {
67 * Implements the magic __toString() method.
69 abstract public function __toString();