4 * Contains \Drupal\bootstrap\Utility\Variables.
7 namespace Drupal\bootstrap\Utility;
10 * Class to help modify template variables.
14 class Variables extends DrupalAttributes {
19 * @var \Drupal\bootstrap\Utility\Element|FALSE
21 public $element = FALSE;
24 * Element constructor.
26 * @param array $variables
27 * A theme hook variables array.
29 public function __construct(array &$variables) {
30 $this->array = &$variables;
31 if (isset($variables['element']) && Element::isRenderArray($variables['element'])) {
32 $this->element = Element::create($variables['element']);
34 elseif (isset($variables['elements']) && Element::isRenderArray($variables['elements'])) {
35 $this->element = Element::create($variables['elements']);
40 * Creates a new \Drupal\bootstrap\Utility\Variables instance.
42 * @param array $variables
43 * A theme hook variables array.
45 * @return \Drupal\bootstrap\Utility\Variables
46 * The newly created variables instance.
48 public static function create(array &$variables) {
49 return new self($variables);
53 * Retrieves a context value from the variables array or its element, if any.
56 * The name of the context key to retrieve.
57 * @param mixed $default
58 * Optional. The default value to use if the context $name isn't set.
61 * The context value or the $default value if not set.
63 public function &getContext($name, $default = NULL) {
64 $context = &$this->offsetGet($this->attributePrefix . 'context', []);
65 if (!isset($context[$name])) {
66 // If there is no context on the variables array but there is an element
67 // present, proxy the method to the element.
69 return $this->element->getContext($name, $default);
71 $context[$name] = $default;
73 return $context[$name];
77 * Maps an element's properties to the variables attributes array.
80 * An associative array whose keys are element property names and whose
81 * values are the variable names to set in the variables array; e.g.,
82 * array('#property_name' => 'variable_name'). If both names are identical
83 * except for the leading '#', then an attribute name value is sufficient
84 * and no property name needs to be specified.
85 * @param bool $overwrite
86 * If the variable exists, it will be overwritten. This does not apply to
87 * attribute arrays, they will always be merged recursively.
91 public function map(array $map, $overwrite = TRUE) {
92 // Immediately return if there is no element in the variable array.
93 if (!$this->element) {
97 // Iterate over each map item.
98 foreach ($map as $property => $variable) {
99 // If the key is numeric, the attribute name needs to be taken over.
100 if (is_int($property)) {
101 $property = $variable;
104 // Merge attributes from the element.
105 if (strpos($property, 'attributes') !== FALSE) {
106 $this->setAttributes($this->element->getAttributes($property)->getArrayCopy(), $variable);
108 // Set normal variable.
109 elseif ($overwrite || !$this->offsetExists($variable)) {
110 $this->offsetSet($variable, $this->element->getProperty($property));