You are here

function template_preprocess_environment_indicator in Environment Indicator 8.3

Same name and namespace in other branches
  1. 4.x environment_indicator.module \template_preprocess_environment_indicator()

Prepares variables for environment indicator element templates.

Default template: environment-indicator.html.twig.

Parameters

array $variables: An associative array containing:

  • element: An associative array containing the properties of the element. Properties used: #title, #value, #description, #required, #attributes.

File

./environment_indicator.module, line 127
Module implementation file.

Code

function template_preprocess_environment_indicator(array &$variables) {
  $element = $variables['element'];
  $variables['title'] = empty($element['#title']) ? '' : $element['#title'];
  $variables['fg_color'] = empty($element['#fg_color']) ? '' : $element['#fg_color'];
  if (!empty($element['#description'])) {
    $variables['description'] = $element['#description'];
  }

  // Ensure #attributes is set.
  $element += [
    '#attributes' => [],
  ];
  $attributes = $element['#attributes'];

  // Make sure to override existing colors.
  $style = empty($attributes['style']) ? '' : $attributes['style'];
  $structured_styles = _environment_indicator_parse_style($style);
  $structured_styles['background-color'] = empty($element['#bg_color']) ? '' : $element['#bg_color'];
  $structured_styles['color'] = empty($element['#fg_color']) ? '' : $element['#fg_color'];

  // Now put everything back together as an string.
  $data = [];
  foreach ($structured_styles as $css_property => $css_value) {
    $data[] = sprintf('%s: %s', $css_property, $css_value);
  }
  $attributes['style'] = implode('; ', $data);
  $attributes['id'] = empty($attributes['id']) ? empty($attributes['#id']) ? 'environment-indicator' : $attributes['#id'] : $attributes['id'];
  $variables['attributes'] = $attributes;
  $variables['switcher'] = isset($element['switcher']) ? $element['switcher'] : '';
}