3 namespace Drupal\Core\Template;
4 use Drupal\Component\Utility\Html;
7 * Defines the base class for an attribute type.
9 * @see \Drupal\Core\Template\Attribute
11 abstract class AttributeValueBase {
14 * Renders '$name=""' if $value is an empty string.
16 * @see \Drupal\Core\Template\AttributeValueBase::render()
18 const RENDER_EMPTY_ATTRIBUTE = TRUE;
28 * The name of the value.
35 * Constructs a \Drupal\Core\Template\AttributeValueBase object.
37 public function __construct($name, $value) {
39 $this->value = $value;
43 * Returns a string representation of the attribute.
45 * While __toString only returns the value in a string form, render()
46 * contains the name of the attribute as well.
49 * The string representation of the attribute.
51 public function render() {
52 $value = (string) $this;
53 if (isset($this->value) && static::RENDER_EMPTY_ATTRIBUTE || !empty($value)) {
54 return Html::escape($this->name) . '="' . $value . '"';
59 * Returns the raw value.
61 public function value() {
66 * Implements the magic __toString() method.
68 abstract public function __toString();