MappingTest.php

Same filename in this branch
  1. 10 core/tests/Drupal/KernelTests/Config/Schema/MappingTest.php
Same filename in other branches
  1. 9 core/modules/views/tests/modules/views_test_data/src/Plugin/views/style/MappingTest.php
  2. 8.9.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/style/MappingTest.php
  3. 11.x core/modules/views/tests/modules/views_test_data/src/Plugin/views/style/MappingTest.php
  4. 11.x core/tests/Drupal/KernelTests/Config/Schema/MappingTest.php

Namespace

Drupal\views_test_data\Plugin\views\style

File

core/modules/views/tests/modules/views_test_data/src/Plugin/views/style/MappingTest.php

View source
<?php

namespace Drupal\views_test_data\Plugin\views\style;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\views\Attribute\ViewsStyle;
use Drupal\views\Plugin\views\style\Mapping;
use Drupal\views\Plugin\views\field\NumericField;

/**
 * Provides a test plugin for the mapping style.
 *
 * @ingroup views_style_plugins
 */
class MappingTest extends Mapping {
    
    /**
     * {@inheritdoc}
     */
    protected function defineMapping() {
        return [
            'title_field' => [
                '#title' => $this->t('Title field'),
                '#description' => $this->t('Choose the field with the custom title.'),
                '#toggle' => TRUE,
                '#required' => TRUE,
            ],
            'name_field' => [
                '#title' => $this->t('Name field'),
                '#description' => $this->t('Choose the field with the custom name.'),
            ],
            'numeric_field' => [
                '#title' => $this->t('Numeric field'),
                '#description' => $this->t('Select one or more numeric fields.'),
                '#multiple' => TRUE,
                '#toggle' => TRUE,
                '#filter' => 'filterNumericFields',
                '#required' => TRUE,
            ],
        ];
    }
    
    /**
     * Restricts the allowed fields to only numeric fields.
     *
     * @param array $fields
     *   An array of field labels, keyed by the field ID.
     */
    protected function filterNumericFields(&$fields) {
        foreach ($this->view->field as $id => $field) {
            if (!$field instanceof NumericField) {
                unset($fields[$id]);
            }
        }
    }

}

Classes

Title Deprecated Summary
MappingTest Provides a test plugin for the mapping style.

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