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