Newer
Older
Tim Plunkett
committed
<?php
/**
* @file
* Contains \Drupal\views_test_data\Plugin\views\style\MappingTest.
Tim Plunkett
committed
*/
namespace Drupal\views_test_data\Plugin\views\style;
use Drupal\views\Plugin\views\style\Mapping;
Alex Pott
committed
use Drupal\views\Plugin\views\field\NumericField;
Tim Plunkett
committed
/**
* Provides a test plugin for the mapping style.
*
* @ingroup views_style_plugins
*
Alex Pott
committed
* @ViewsStyle(
Tim Plunkett
committed
* id = "mapping_test",
* title = @Translation("Field mapping"),
* help = @Translation("Maps specific fields to specific purposes."),
* theme = "views_view_mapping_test",
* display_types = {"normal", "test"}
Tim Plunkett
committed
* )
*/
class MappingTest extends Mapping {
/**
* {@inheritdoc}
Tim Plunkett
committed
*/
protected function defineMapping() {
return array(
'title_field' => array(
'#title' => $this->t('Title field'),
'#description' => $this->t('Choose the field with the custom title.'),
Tim Plunkett
committed
'#toggle' => TRUE,
'#required' => TRUE,
),
'name_field' => array(
'#title' => $this->t('Name field'),
'#description' => $this->t('Choose the field with the custom name.'),
Tim Plunkett
committed
),
'numeric_field' => array(
'#title' => $this->t('Numeric field'),
'#description' => $this->t('Select one or more numeric fields.'),
Tim Plunkett
committed
'#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) {
Alex Pott
committed
if (!($field instanceof NumericField)) {
Tim Plunkett
committed
unset($fields[$id]);
}
}
}
}