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.
26 protected $createdColumn = 'node_field_data-created';
29 * Set default values for the filters.
31 protected $filters = [
34 'table' => 'node_field_data',
36 'plugin_id' => 'boolean',
37 'entity_type' => 'node',
38 'entity_field' => 'status',
43 * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
46 * An array whose keys are the available sort options and whose
47 * corresponding values are human readable labels.
49 public function getAvailableSorts() {
50 // You can't execute functions in properties, so override the method
52 'node_field_data-title:ASC' => $this->t('Title')
59 protected function rowStyleOptions() {
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');
72 protected function defaultDisplayOptions() {
73 $display_options = parent::defaultDisplayOptions();
75 // Add permission-based access control.
76 $display_options['access']['type'] = 'perm';
77 $display_options['access']['options']['perm'] = 'access content';
79 // Remove the default fields, since we are customizing them here.
80 unset($display_options['fields']);
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';
104 return $display_options;
110 protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
111 $filters = parent::defaultDisplayFiltersUser($form, $form_state);
114 if ($values = $form_state->getValue(['show', 'tagged_with'])) {
115 foreach ($values as $value) {
116 $tids[] = $value['target_id'];
120 $vid = reset($form['displays']['show']['tagged_with']['#selection_settings']['target_bundles']);
123 'table' => 'taxonomy_index',
127 'plugin_id' => 'taxonomy_index_tid',
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']);
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;
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;
165 * Set the row style and row style plugins to the display_options.
167 protected function display_options_row(&$display_options, $row_plugin, $row_options) {
168 switch ($row_plugin) {
170 $display_options['row']['type'] = 'entity:node';
171 $display_options['row']['options']['view_mode'] = 'full';
174 $display_options['row']['type'] = 'entity:node';
175 $display_options['row']['options']['view_mode'] = 'teaser';
177 case 'titles_linked':
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';
190 * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
192 * Add some options for filter by taxonomy terms.
194 protected function buildFilters(&$form, FormStateInterface $form_state) {
195 parent::buildFilters($form, $form_state);
197 if (isset($form['displays']['show']['type'])) {
198 $selected_bundle = static::getSelected($form_state, ['show', 'type'], 'all', $form['displays']['show']['type']);
201 // Add the "tagged with" filter to the view.
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
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
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];
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';
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;
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';
252 $tag_field_name = key($tag_fields);
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],
263 '#maxlength' => 1024,