You are here

function template_preprocess_field__flag in farmOS 2.x

Prepares variables for field--flag templates.

Adds classes to each flag wrapper.

Default template: field--flag.html.twig.

Parameters

array $variables: An associative array containing:

  • element: An associative array containing render arrays for the list of flags.

File

modules/core/flag/farm_flag.module, line 132
The farmOS Flags module.

Code

function template_preprocess_field__flag(array &$variables) {

  // Preprocess list_string flag fields.
  if ($variables['element']['#field_type'] == 'list_string') {

    /** @var \Drupal\Core\Field\FieldItemListInterface $items */
    $items = $variables['element']['#items'];

    // Add classes to each flag.
    foreach ($items as $key => $list_item) {
      $classes = [
        'flag',
        'flag--' . $list_item
          ->getString(),
      ];
      $variables['items'][$key]['attributes']
        ->addClass($classes);
    }
  }
}