3 namespace Drupal\views\Plugin\views\style;
5 use Drupal\Core\Form\FormStateInterface;
8 * Allows fields to be mapped to specific use cases.
10 * @ingroup views_style_plugins
12 abstract class Mapping extends StylePluginBase {
15 * Do not use grouping.
19 protected $usesGrouping = FALSE;
22 * Use fields without a row plugin.
26 protected $usesFields = TRUE;
29 * Builds the list of field mappings.
32 * An associative array, keyed by the field name, containing the following
34 * - #title: The human-readable label for this field.
35 * - #default_value: The default value for this field. If not provided, an
36 * empty string will be used.
37 * - #description: A description of this field.
38 * - #required: Whether this field is required.
39 * - #filter: (optional) A method on the plugin to filter field options.
40 * - #toggle: (optional) If this select should be toggled by a checkbox.
42 abstract protected function defineMapping();
47 protected function defineOptions() {
48 $options = parent::defineOptions();
50 // Parse the mapping and add a default for each.
51 foreach ($this->defineMapping() as $key => $value) {
52 $default = !empty($value['#multiple']) ? [] : '';
53 $options['mapping']['contains'][$key] = [
54 'default' => isset($value['#default_value']) ? $value['#default_value'] : $default,
56 if (!empty($value['#toggle'])) {
57 $options['mapping']['contains']["toggle_$key"] = [
69 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
70 parent::buildOptionsForm($form, $form_state);
73 $mapping = $this->defineMapping();
75 // Restrict the list of defaults to the mapping, in case they have changed.
76 $options = array_intersect_key($this->options['mapping'], $mapping);
78 // Get the labels of the fields added to this display.
79 $field_labels = $this->displayHandler->getFieldLabels();
81 // Provide some default values.
88 // For each mapping, add a select element to the form.
89 foreach ($options as $key => $value) {
90 // If the field is optional, add a 'None' value to the top of the options.
92 $required = !empty($mapping[$key]['#required']);
93 if (!$required && empty($mapping[$key]['#multiple'])) {
94 $field_options = ['' => $this->t('- None -')];
96 $field_options += $field_labels;
98 // Optionally filter the available fields.
99 if (isset($mapping[$key]['#filter'])) {
100 $this->view->initHandlers();
101 $filter = $mapping[$key]['#filter'];
102 $this::$filter($field_options);
103 unset($mapping[$key]['#filter']);
106 // These values must always be set.
108 '#options' => $field_options,
109 '#default_value' => $options[$key],
112 // Optionally allow the select to be toggleable.
113 if (!empty($mapping[$key]['#toggle'])) {
114 $form['mapping']["toggle_$key"] = [
115 '#type' => 'checkbox',
116 '#title' => $this->t('Use a custom %field_name', ['%field_name' => strtolower($mapping[$key]['#title'])]),
117 '#default_value' => $this->options['mapping']["toggle_$key"],
119 $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = ['checked' => TRUE];
122 $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
127 * Overrides Drupal\views\Plugin\views\style\StylePluginBase::render().
129 * Provides the mapping definition as an available variable.
131 public function render() {
133 '#theme' => $this->themeFunctions(),
134 '#view' => $this->view,
135 '#options' => $this->options,
136 '#rows' => $this->view->result,
137 '#mapping' => $this->defineMapping(),