Backup of db before drupal security update
[yaffs-website] / web / core / modules / views / src / Plugin / views / style / Mapping.php
1 <?php
2
3 namespace Drupal\views\Plugin\views\style;
4
5 use Drupal\Core\Form\FormStateInterface;
6
7 /**
8  * Allows fields to be mapped to specific use cases.
9  *
10  * @ingroup views_style_plugins
11  */
12 abstract class Mapping extends StylePluginBase {
13
14   /**
15    * Do not use grouping.
16    *
17    * @var bool
18    */
19   protected $usesGrouping = FALSE;
20
21   /**
22    * Use fields without a row plugin.
23    *
24    * @var bool
25    */
26   protected $usesFields = TRUE;
27
28   /**
29    * Builds the list of field mappings.
30    *
31    * @return array
32    *   An associative array, keyed by the field name, containing the following
33    *   key-value pairs:
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.
41    */
42   abstract protected function defineMapping();
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function defineOptions() {
48     $options = parent::defineOptions();
49
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,
55       ];
56       if (!empty($value['#toggle'])) {
57         $options['mapping']['contains']["toggle_$key"] = [
58           'default' => FALSE,
59         ];
60       }
61     }
62
63     return $options;
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
70     parent::buildOptionsForm($form, $form_state);
71
72     // Get the mapping.
73     $mapping = $this->defineMapping();
74
75     // Restrict the list of defaults to the mapping, in case they have changed.
76     $options = array_intersect_key($this->options['mapping'], $mapping);
77
78     // Get the labels of the fields added to this display.
79     $field_labels = $this->displayHandler->getFieldLabels();
80
81     // Provide some default values.
82     $defaults = [
83       '#type' => 'select',
84       '#required' => FALSE,
85       '#multiple' => FALSE,
86     ];
87
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.
91       $field_options = [];
92       $required = !empty($mapping[$key]['#required']);
93       if (!$required && empty($mapping[$key]['#multiple'])) {
94         $field_options = ['' => $this->t('- None -')];
95       }
96       $field_options += $field_labels;
97
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']);
104       }
105
106       // These values must always be set.
107       $overrides = [
108         '#options' => $field_options,
109         '#default_value' => $options[$key],
110       ];
111
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"],
118         ];
119         $overrides['#states']['visible'][':input[name="style_options[mapping][' . "toggle_$key" . ']"]'] = ['checked' => TRUE];
120       }
121
122       $form['mapping'][$key] = $overrides + $mapping[$key] + $defaults;
123     }
124   }
125
126   /**
127    * Overrides Drupal\views\Plugin\views\style\StylePluginBase::render().
128    *
129    * Provides the mapping definition as an available variable.
130    */
131   public function render() {
132     return [
133       '#theme' => $this->themeFunctions(),
134       '#view' => $this->view,
135       '#options' => $this->options,
136       '#rows' => $this->view->result,
137       '#mapping' => $this->defineMapping(),
138     ];
139   }
140
141 }