You are here

public function Variables::map in Express 8

Maps an element's properties to the variables attributes array.

Parameters

array $map: An associative array whose keys are element property names and whose values are the variable names to set in the variables array; e.g., array('#property_name' => 'variable_name'). If both names are identical except for the leading '#', then an attribute name value is sufficient and no property name needs to be specified.

bool $overwrite: If the variable exists, it will be overwritten. This does not apply to attribute arrays, they will always be merged recursively.

Return value

$this

File

themes/contrib/bootstrap/src/Utility/Variables.php, line 91
Contains \Drupal\bootstrap\Utility\Variables.

Class

Variables
Class to help modify template variables.

Namespace

Drupal\bootstrap\Utility

Code

public function map(array $map, $overwrite = TRUE) {

  // Immediately return if there is no element in the variable array.
  if (!$this->element) {
    return $this;
  }

  // Iterate over each map item.
  foreach ($map as $property => $variable) {

    // If the key is numeric, the attribute name needs to be taken over.
    if (is_int($property)) {
      $property = $variable;
    }

    // Merge attributes from the element.
    if (strpos($property, 'attributes') !== FALSE) {
      $this
        ->setAttributes($this->element
        ->getAttributes($property)
        ->getArrayCopy(), $variable);
    }
    elseif ($overwrite || !$this
      ->offsetExists($variable)) {
      $this
        ->offsetSet($variable, $this->element
        ->getProperty($property));
    }
  }
  return $this;
}