function template_preprocess_views_view_mapping_test

Same name and namespace in other branches
  1. 9 core/modules/views/tests/modules/views_test_data/views_test_data.module \template_preprocess_views_view_mapping_test()
  2. 8.9.x core/modules/views/tests/modules/views_test_data/views_test_data.module \template_preprocess_views_view_mapping_test()
  3. 10 core/modules/views/tests/modules/views_test_data/views_test_data.module \template_preprocess_views_view_mapping_test()

Prepares variables for the mapping row style test templates.

Default template: views-view-mapping-test.html.twig.

Parameters

array $variables: An associative array containing:

  • rows: A list of view rows.
  • options: Various view options, including the row style mapping.
  • view: The view object.

File

core/modules/views/tests/modules/views_test_data/views_test_data.module, line 68

Code

function template_preprocess_views_view_mapping_test(&$variables) : void {
  $variables['element'] = [];
  foreach ($variables['rows'] as $delta => $row) {
    $fields = [];
    foreach ($variables['options']['mapping'] as $type => $field_names) {
      if (!is_array($field_names)) {
        $field_names = [
          $field_names,
        ];
      }
      foreach ($field_names as $field_name) {
        if ($value = $variables['view']->style_plugin
          ->getField($delta, $field_name)) {
          $fields[$type . '-' . $field_name] = $type . ':' . $value;
        }
      }
    }
    // If there are no fields in this row, skip to the next one.
    if (empty($fields)) {
      continue;
    }
    // Build a container for the row.
    $variables['element'][$delta] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'views-row-mapping-test',
        ],
      ],
    ];
    // Add each field to the row.
    foreach ($fields as $key => $render) {
      $variables['element'][$delta][$key] = [
        '#children' => $render,
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            $key,
          ],
        ],
      ];
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.