You are here

protected function ViewsDuplicateBuilderBase::replaceTemplateKeyAndValues in Views Templates 8

Recursively replace keys and values in template elements.

For example of builder and yml template:

Parameters

array $template_elements: Array of elements from a View Template array.

array $replace_values: The values in that should be replaced in the template. The keys in this array can be keys OR values template array. This allows replacing both keys and values in the template.

mixed $options: The options to replace template key and values.

See also

Drupal\views_templates_builder_test\Plugin\ViewsTemplateBuilder

1 call to ViewsDuplicateBuilderBase::replaceTemplateKeyAndValues()
ViewsDuplicateBuilderBase::alterViewTemplateAfterCreation in src/Plugin/ViewsDuplicateBuilderBase.php
After View Template has been created the Builder should alter it some how.

File

src/Plugin/ViewsDuplicateBuilderBase.php, line 192

Class

ViewsDuplicateBuilderBase
ViewsDuplicateBuilderBase Class.

Namespace

Drupal\views_templates\Plugin

Code

protected function replaceTemplateKeyAndValues(array &$template_elements, array $replace_values, $options = NULL) {
  foreach ($template_elements as $key => &$value) {
    if (is_array($value)) {
      $this
        ->replaceTemplateKeyAndValues($value, $replace_values, $options);
    }
    foreach ($replace_values as $replace_key => $replace_value) {
      if (!is_array($value)) {
        if (is_string($value)) {
          if (stripos($value, $replace_key) !== FALSE) {
            $value = str_replace($replace_key, $replace_value, $value);
          }
        }
        elseif ($replace_key === $value) {
          $value = $replace_value;
        }
      }
      if (stripos($key, $replace_key) !== FALSE) {
        $new_key = str_replace($replace_key, $replace_value, $key);

        // NULL is used in replace value to remove keys from template.
        if ($replace_value !== NULL) {
          $template_elements[$new_key] = $value;
        }
        unset($template_elements[$key]);
      }
    }
  }
}