3 namespace Drupal\node\Plugin\views\wizard;
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\views\Plugin\views\wizard\WizardPluginBase;
9 * @todo: replace numbers with constants.
13 * Tests creating node views with the wizard.
17 * base_table = "node_field_data",
18 * title = @Translation("Content")
21 class Node extends WizardPluginBase {
24 * Set the created column.
28 protected $createdColumn = 'node_field_data-created';
31 * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
34 * An array whose keys are the available sort options and whose
35 * corresponding values are human readable labels.
37 public function getAvailableSorts() {
38 // You can't execute functions in properties, so override the method
40 'node_field_data-title:ASC' => $this->t('Title')
47 protected function rowStyleOptions() {
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');
60 protected function defaultDisplayOptions() {
61 $display_options = parent::defaultDisplayOptions();
63 // Add permission-based access control.
64 $display_options['access']['type'] = 'perm';
65 $display_options['access']['options']['perm'] = 'access content';
67 // Remove the default fields, since we are customizing them here.
68 unset($display_options['fields']);
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';
92 return $display_options;
98 protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
99 $filters = parent::defaultDisplayFiltersUser($form, $form_state);
102 if ($values = $form_state->getValue(['show', 'tagged_with'])) {
103 foreach ($values as $value) {
104 $tids[] = $value['target_id'];
108 $vid = reset($form['displays']['show']['tagged_with']['#selection_settings']['target_bundles']);
111 'table' => 'taxonomy_index',
115 'plugin_id' => 'taxonomy_index_tid',
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']);
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;
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;
153 * Set the row style and row style plugins to the display_options.
155 protected function display_options_row(&$display_options, $row_plugin, $row_options) {
156 switch ($row_plugin) {
158 $display_options['row']['type'] = 'entity:node';
159 $display_options['row']['options']['view_mode'] = 'full';
162 $display_options['row']['type'] = 'entity:node';
163 $display_options['row']['options']['view_mode'] = 'teaser';
165 case 'titles_linked':
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';
178 * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
180 * Add some options for filter by taxonomy terms.
182 protected function buildFilters(&$form, FormStateInterface $form_state) {
183 parent::buildFilters($form, $form_state);
185 if (isset($form['displays']['show']['type'])) {
186 $selected_bundle = static::getSelected($form_state, ['show', 'type'], 'all', $form['displays']['show']['type']);
189 // Add the "tagged with" filter to the view.
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
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
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];
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';
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;
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';
240 $tag_field_name = key($tag_fields);
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],
251 '#maxlength' => 1024,