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