Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / node / src / Plugin / views / wizard / Node.php
1 <?php
2
3 namespace Drupal\node\Plugin\views\wizard;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\wizard\WizardPluginBase;
7
8 /**
9  * @todo: replace numbers with constants.
10  */
11
12 /**
13  * Tests creating node views with the wizard.
14  *
15  * @ViewsWizard(
16  *   id = "node",
17  *   base_table = "node_field_data",
18  *   title = @Translation("Content")
19  * )
20  */
21 class Node extends WizardPluginBase {
22
23   /**
24    * Set the created column.
25    *
26    * @var string
27    */
28   protected $createdColumn = 'node_field_data-created';
29
30   /**
31    * Set default values for the filters.
32    */
33   protected $filters = [
34     'status' => [
35       'value' => TRUE,
36       'table' => 'node_field_data',
37       'field' => 'status',
38       'plugin_id' => 'boolean',
39       'entity_type' => 'node',
40       'entity_field' => 'status',
41     ]
42   ];
43
44   /**
45    * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
46    *
47    * @return array
48    *   An array whose keys are the available sort options and whose
49    *   corresponding values are human readable labels.
50    */
51   public function getAvailableSorts() {
52     // You can't execute functions in properties, so override the method
53     return [
54       'node_field_data-title:ASC' => $this->t('Title')
55     ];
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   protected function rowStyleOptions() {
62     $options = [];
63     $options['teasers'] = $this->t('teasers');
64     $options['full_posts'] = $this->t('full posts');
65     $options['titles'] = $this->t('titles');
66     $options['titles_linked'] = $this->t('titles (linked)');
67     $options['fields'] = $this->t('fields');
68     return $options;
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   protected function defaultDisplayOptions() {
75     $display_options = parent::defaultDisplayOptions();
76
77     // Add permission-based access control.
78     $display_options['access']['type'] = 'perm';
79     $display_options['access']['options']['perm'] = 'access content';
80
81     // Remove the default fields, since we are customizing them here.
82     unset($display_options['fields']);
83
84     // Add the title field, so that the display has content if the user switches
85     // to a row style that uses fields.
86     /* Field: Content: Title */
87     $display_options['fields']['title']['id'] = 'title';
88     $display_options['fields']['title']['table'] = 'node_field_data';
89     $display_options['fields']['title']['field'] = 'title';
90     $display_options['fields']['title']['entity_type'] = 'node';
91     $display_options['fields']['title']['entity_field'] = 'title';
92     $display_options['fields']['title']['label'] = '';
93     $display_options['fields']['title']['alter']['alter_text'] = 0;
94     $display_options['fields']['title']['alter']['make_link'] = 0;
95     $display_options['fields']['title']['alter']['absolute'] = 0;
96     $display_options['fields']['title']['alter']['trim'] = 0;
97     $display_options['fields']['title']['alter']['word_boundary'] = 0;
98     $display_options['fields']['title']['alter']['ellipsis'] = 0;
99     $display_options['fields']['title']['alter']['strip_tags'] = 0;
100     $display_options['fields']['title']['alter']['html'] = 0;
101     $display_options['fields']['title']['hide_empty'] = 0;
102     $display_options['fields']['title']['empty_zero'] = 0;
103     $display_options['fields']['title']['settings']['link_to_entity'] = 1;
104     $display_options['fields']['title']['plugin_id'] = 'field';
105
106     return $display_options;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
113     $filters = parent::defaultDisplayFiltersUser($form, $form_state);
114
115     $tids = [];
116     if ($values = $form_state->getValue(['show', 'tagged_with'])) {
117       foreach ($values as $value) {
118         $tids[] = $value['target_id'];
119       }
120     }
121     if (!empty($tids)) {
122       $vid = reset($form['displays']['show']['tagged_with']['#selection_settings']['target_bundles']);
123       $filters['tid'] = [
124         'id' => 'tid',
125         'table' => 'taxonomy_index',
126         'field' => 'tid',
127         'value' => $tids,
128         'vid' => $vid,
129         'plugin_id' => 'taxonomy_index_tid',
130       ];
131       // If the user entered more than one valid term in the autocomplete
132       // field, they probably intended both of them to be applied.
133       if (count($tids) > 1) {
134         $filters['tid']['operator'] = 'and';
135         // Sort the terms so the filter will be displayed as it normally would
136         // on the edit screen.
137         sort($filters['tid']['value']);
138       }
139     }
140
141     return $filters;
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   protected function pageDisplayOptions(array $form, FormStateInterface $form_state) {
148     $display_options = parent::pageDisplayOptions($form, $form_state);
149     $row_plugin = $form_state->getValue(['page', 'style', 'row_plugin']);
150     $row_options = $form_state->getValue(['page', 'style', 'row_options'], []);
151     $this->display_options_row($display_options, $row_plugin, $row_options);
152     return $display_options;
153   }
154
155   /**
156    * {@inheritdoc}
157    */
158   protected function blockDisplayOptions(array $form, FormStateInterface $form_state) {
159     $display_options = parent::blockDisplayOptions($form, $form_state);
160     $row_plugin = $form_state->getValue(['block', 'style', 'row_plugin']);
161     $row_options = $form_state->getValue(['block', 'style', 'row_options'], []);
162     $this->display_options_row($display_options, $row_plugin, $row_options);
163     return $display_options;
164   }
165
166   /**
167    * Set the row style and row style plugins to the display_options.
168    */
169   protected  function display_options_row(&$display_options, $row_plugin, $row_options) {
170     switch ($row_plugin) {
171       case 'full_posts':
172         $display_options['row']['type'] = 'entity:node';
173         $display_options['row']['options']['view_mode'] = 'full';
174         break;
175       case 'teasers':
176         $display_options['row']['type'] = 'entity:node';
177         $display_options['row']['options']['view_mode'] = 'teaser';
178         break;
179       case 'titles_linked':
180       case 'titles':
181         $display_options['row']['type'] = 'fields';
182         $display_options['fields']['title']['id'] = 'title';
183         $display_options['fields']['title']['table'] = 'node_field_data';
184         $display_options['fields']['title']['field'] = 'title';
185         $display_options['fields']['title']['settings']['link_to_entity'] = $row_plugin === 'titles_linked';
186         $display_options['fields']['title']['plugin_id'] = 'field';
187         break;
188     }
189   }
190
191   /**
192    * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
193    *
194    * Add some options for filter by taxonomy terms.
195    */
196   protected function buildFilters(&$form, FormStateInterface $form_state) {
197     parent::buildFilters($form, $form_state);
198
199     if (isset($form['displays']['show']['type'])) {
200       $selected_bundle = static::getSelected($form_state, ['show', 'type'], 'all', $form['displays']['show']['type']);
201     }
202
203     // Add the "tagged with" filter to the view.
204
205     // We construct this filter using taxonomy_index.tid (which limits the
206     // filtering to a specific vocabulary) rather than
207     // taxonomy_term_field_data.name (which matches terms in any vocabulary).
208     // This is because it is a more commonly-used filter that works better with
209     // the autocomplete UI, and also to avoid confusion with other vocabularies
210     // on the site that may have terms with the same name but are not used for
211     // free tagging.
212
213     // The downside is that if there *is* more than one vocabulary on the site
214     // that is used for free tagging, the wizard will only be able to make the
215     // "tagged with" filter apply to one of them (see below for the method it
216     // uses to choose).
217
218     // Find all "tag-like" taxonomy fields associated with the view's
219     // entities. If a particular entity type (i.e., bundle) has been
220     // selected above, then we only search for taxonomy fields associated
221     // with that bundle. Otherwise, we use all bundles.
222     $bundles = array_keys($this->bundleInfoService->getBundleInfo($this->entityTypeId));
223     // Double check that this is a real bundle before using it (since above
224     // we added a dummy option 'all' to the bundle list on the form).
225     if (isset($selected_bundle) && in_array($selected_bundle, $bundles)) {
226       $bundles = [$selected_bundle];
227     }
228     $tag_fields = [];
229     foreach ($bundles as $bundle) {
230       $display = entity_get_form_display($this->entityTypeId, $bundle, 'default');
231       $taxonomy_fields = array_filter(\Drupal::entityManager()->getFieldDefinitions($this->entityTypeId, $bundle), function ($field_definition) {
232         return $field_definition->getType() == 'entity_reference' && $field_definition->getSetting('target_type') == 'taxonomy_term';
233       });
234       foreach ($taxonomy_fields as $field_name => $field) {
235         $widget = $display->getComponent($field_name);
236         // We define "tag-like" taxonomy fields as ones that use the
237         // "Autocomplete (Tags style)" widget.
238         if ($widget['type'] == 'entity_reference_autocomplete_tags') {
239           $tag_fields[$field_name] = $field;
240         }
241       }
242     }
243     if (!empty($tag_fields)) {
244       // If there is more than one "tag-like" taxonomy field available to
245       // the view, we can only make our filter apply to one of them (as
246       // described above). We choose 'field_tags' if it is available, since
247       // that is created by the Standard install profile in core and also
248       // commonly used by contrib modules; thus, it is most likely to be
249       // associated with the "main" free-tagging vocabulary on the site.
250       if (array_key_exists('field_tags', $tag_fields)) {
251         $tag_field_name = 'field_tags';
252       }
253       else {
254         $tag_field_name = key($tag_fields);
255       }
256       // Add the autocomplete textfield to the wizard.
257       $target_bundles = $tag_fields[$tag_field_name]->getSetting('handler_settings')['target_bundles'];
258       $form['displays']['show']['tagged_with'] = [
259         '#type' => 'entity_autocomplete',
260         '#title' => $this->t('tagged with'),
261         '#target_type' => 'taxonomy_term',
262         '#selection_settings' => ['target_bundles' => $target_bundles],
263         '#tags' => TRUE,
264         '#size' => 30,
265         '#maxlength' => 1024,
266       ];
267     }
268   }
269
270 }