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 * Set default values for the filters.
33 protected $filters = [
36 'table' => 'node_field_data',
38 'plugin_id' => 'boolean',
39 'entity_type' => 'node',
40 'entity_field' => 'status',
45 * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
48 * An array whose keys are the available sort options and whose
49 * corresponding values are human readable labels.
51 public function getAvailableSorts() {
52 // You can't execute functions in properties, so override the method
54 'node_field_data-title:ASC' => $this->t('Title')
61 protected function rowStyleOptions() {
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');
74 protected function defaultDisplayOptions() {
75 $display_options = parent::defaultDisplayOptions();
77 // Add permission-based access control.
78 $display_options['access']['type'] = 'perm';
79 $display_options['access']['options']['perm'] = 'access content';
81 // Remove the default fields, since we are customizing them here.
82 unset($display_options['fields']);
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';
106 return $display_options;
112 protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
113 $filters = parent::defaultDisplayFiltersUser($form, $form_state);
116 if ($values = $form_state->getValue(['show', 'tagged_with'])) {
117 foreach ($values as $value) {
118 $tids[] = $value['target_id'];
122 $vid = reset($form['displays']['show']['tagged_with']['#selection_settings']['target_bundles']);
125 'table' => 'taxonomy_index',
129 'plugin_id' => 'taxonomy_index_tid',
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']);
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;
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;
167 * Set the row style and row style plugins to the display_options.
169 protected function display_options_row(&$display_options, $row_plugin, $row_options) {
170 switch ($row_plugin) {
172 $display_options['row']['type'] = 'entity:node';
173 $display_options['row']['options']['view_mode'] = 'full';
176 $display_options['row']['type'] = 'entity:node';
177 $display_options['row']['options']['view_mode'] = 'teaser';
179 case 'titles_linked':
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';
192 * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
194 * Add some options for filter by taxonomy terms.
196 protected function buildFilters(&$form, FormStateInterface $form_state) {
197 parent::buildFilters($form, $form_state);
199 if (isset($form['displays']['show']['type'])) {
200 $selected_bundle = static::getSelected($form_state, ['show', 'type'], 'all', $form['displays']['show']['type']);
203 // Add the "tagged with" filter to the view.
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
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
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];
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';
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;
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';
254 $tag_field_name = key($tag_fields);
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],
265 '#maxlength' => 1024,