Node.php 9.73 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2
<?php

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 10

/**
 * @todo: replace numbers with constants.
 */
11

merlinofchaos's avatar
merlinofchaos committed
12 13
/**
 * Tests creating node views with the wizard.
14
 *
15
 * @ViewsWizard(
aspilicious's avatar
aspilicious committed
16
 *   id = "node",
17
 *   base_table = "node_field_data",
18
 *   title = @Translation("Content")
19
 * )
merlinofchaos's avatar
merlinofchaos committed
20
 */
21
class Node extends WizardPluginBase {
merlinofchaos's avatar
merlinofchaos committed
22

23 24
  /**
   * Set the created column.
25 26
   *
   * @var string
27
   */
28
  protected $createdColumn = 'node_field_data-created';
29 30 31 32 33

  /**
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
   *
   * @return array
34 35
   *   An array whose keys are the available sort options and whose
   *   corresponding values are human readable labels.
36 37 38
   */
  public function getAvailableSorts() {
    // 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 50 51 52 53
    $options['teasers'] = $this->t('teasers');
    $options['full_posts'] = $this->t('full posts');
    $options['titles'] = $this->t('titles');
    $options['titles_linked'] = $this->t('titles (linked)');
    $options['fields'] = $this->t('fields');
merlinofchaos's avatar
merlinofchaos committed
54 55 56 57
    return $options;
  }

  /**
58
   * {@inheritdoc}
merlinofchaos's avatar
merlinofchaos committed
59
   */
60 61
  protected function defaultDisplayOptions() {
    $display_options = parent::defaultDisplayOptions();
merlinofchaos's avatar
merlinofchaos committed
62 63 64

    // Add permission-based access control.
    $display_options['access']['type'] = 'perm';
65
    $display_options['access']['options']['perm'] = 'access content';
merlinofchaos's avatar
merlinofchaos committed
66 67 68 69 70 71 72 73

    // Remove the default fields, since we are customizing them here.
    unset($display_options['fields']);

    // Add the title field, so that the display has content if the user switches
    // to a row style that uses fields.
    /* Field: Content: Title */
    $display_options['fields']['title']['id'] = 'title';
74
    $display_options['fields']['title']['table'] = 'node_field_data';
merlinofchaos's avatar
merlinofchaos committed
75
    $display_options['fields']['title']['field'] = 'title';
76 77
    $display_options['fields']['title']['entity_type'] = 'node';
    $display_options['fields']['title']['entity_field'] = 'title';
merlinofchaos's avatar
merlinofchaos committed
78 79 80 81 82 83 84 85 86 87 88
    $display_options['fields']['title']['label'] = '';
    $display_options['fields']['title']['alter']['alter_text'] = 0;
    $display_options['fields']['title']['alter']['make_link'] = 0;
    $display_options['fields']['title']['alter']['absolute'] = 0;
    $display_options['fields']['title']['alter']['trim'] = 0;
    $display_options['fields']['title']['alter']['word_boundary'] = 0;
    $display_options['fields']['title']['alter']['ellipsis'] = 0;
    $display_options['fields']['title']['alter']['strip_tags'] = 0;
    $display_options['fields']['title']['alter']['html'] = 0;
    $display_options['fields']['title']['hide_empty'] = 0;
    $display_options['fields']['title']['empty_zero'] = 0;
89 90
    $display_options['fields']['title']['settings']['link_to_entity'] = 1;
    $display_options['fields']['title']['plugin_id'] = 'field';
merlinofchaos's avatar
merlinofchaos committed
91 92 93 94

    return $display_options;
  }

95
  /**
96
   * {@inheritdoc}
97
   */
98
  protected function defaultDisplayFiltersUser(array $form, FormStateInterface $form_state) {
99
    $filters = parent::defaultDisplayFiltersUser($form, $form_state);
100

101 102
    $tids = [];
    if ($values = $form_state->getValue(['show', 'tagged_with'])) {
103 104 105 106
      foreach ($values as $value) {
        $tids[] = $value['target_id'];
      }
    }
107
    if (!empty($tids)) {
108
      $vid = reset($form['displays']['show']['tagged_with']['#selection_settings']['target_bundles']);
109
      $filters['tid'] = [
110 111 112
        'id' => 'tid',
        'table' => 'taxonomy_index',
        'field' => 'tid',
113
        'value' => $tids,
114
        'vid' => $vid,
115
        'plugin_id' => 'taxonomy_index_tid',
116
      ];
117 118
      // If the user entered more than one valid term in the autocomplete
      // field, they probably intended both of them to be applied.
119
      if (count($tids) > 1) {
120 121 122 123 124 125 126 127 128 129
        $filters['tid']['operator'] = 'and';
        // Sort the terms so the filter will be displayed as it normally would
        // on the edit screen.
        sort($filters['tid']['value']);
      }
    }

    return $filters;
  }

130
  /**
131
   * {@inheritdoc}
132
   */
133
  protected function pageDisplayOptions(array $form, FormStateInterface $form_state) {
134
    $display_options = parent::pageDisplayOptions($form, $form_state);
135 136
    $row_plugin = $form_state->getValue(['page', 'style', 'row_plugin']);
    $row_options = $form_state->getValue(['page', 'style', 'row_options'], []);
merlinofchaos's avatar
merlinofchaos committed
137 138 139 140
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

141
  /**
142
   * {@inheritdoc}
143
   */
144
  protected function blockDisplayOptions(array $form, FormStateInterface $form_state) {
145
    $display_options = parent::blockDisplayOptions($form, $form_state);
146 147
    $row_plugin = $form_state->getValue(['block', 'style', 'row_plugin']);
    $row_options = $form_state->getValue(['block', 'style', 'row_options'], []);
merlinofchaos's avatar
merlinofchaos committed
148 149 150 151 152 153 154 155 156 157
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

  /**
   * Set the row style and row style plugins to the display_options.
   */
  protected  function display_options_row(&$display_options, $row_plugin, $row_options) {
    switch ($row_plugin) {
      case 'full_posts':
158
        $display_options['row']['type'] = 'entity:node';
159
        $display_options['row']['options']['view_mode'] = 'full';
merlinofchaos's avatar
merlinofchaos committed
160 161
        break;
      case 'teasers':
162
        $display_options['row']['type'] = 'entity:node';
163
        $display_options['row']['options']['view_mode'] = 'teaser';
merlinofchaos's avatar
merlinofchaos committed
164 165 166
        break;
      case 'titles_linked':
      case 'titles':
167
        $display_options['row']['type'] = 'fields';
168 169 170
        $display_options['fields']['title']['id'] = 'title';
        $display_options['fields']['title']['table'] = 'node_field_data';
        $display_options['fields']['title']['field'] = 'title';
171 172
        $display_options['fields']['title']['settings']['link_to_entity'] = $row_plugin === 'titles_linked';
        $display_options['fields']['title']['plugin_id'] = 'field';
merlinofchaos's avatar
merlinofchaos committed
173 174 175
        break;
    }
  }
176

177
  /**
178
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
179 180 181
   *
   * Add some options for filter by taxonomy terms.
   */
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 190 191

    // Add the "tagged with" filter to the view.

    // We construct this filter using taxonomy_index.tid (which limits the
192 193 194 195 196 197
    // filtering to a specific vocabulary) rather than
    // taxonomy_term_field_data.name (which matches terms in any vocabulary).
    // This is because it is a more commonly-used filter that works better with
    // the autocomplete UI, and also to avoid confusion with other vocabularies
    // on the site that may have terms with the same name but are not used for
    // free tagging.
198 199 200 201 202 203 204 205 206 207

    // The downside is that if there *is* more than one vocabulary on the site
    // that is used for free tagging, the wizard will only be able to make the
    // "tagged with" filter apply to one of them (see below for the method it
    // uses to choose).

    // Find all "tag-like" taxonomy fields associated with the view's
    // entities. If a particular entity type (i.e., bundle) has been
    // selected above, then we only search for taxonomy fields associated
    // with that bundle. Otherwise, we use all bundles.
208
    $bundles = array_keys($this->bundleInfoService->getBundleInfo($this->entityTypeId));
209 210 211
    // Double check that this is a real bundle before using it (since above
    // we added a dummy option 'all' to the bundle list on the form).
    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 224 225
        // "Autocomplete (Tags style)" widget.
        if ($widget['type'] == 'entity_reference_autocomplete_tags') {
          $tag_fields[$field_name] = $field;
226 227 228 229 230 231 232 233 234 235
        }
      }
    }
    if (!empty($tag_fields)) {
      // If there is more than one "tag-like" taxonomy field available to
      // the view, we can only make our filter apply to one of them (as
      // described above). We choose 'field_tags' if it is available, since
      // that is created by the Standard install profile in core and also
      // commonly used by contrib modules; thus, it is most likely to be
      // associated with the "main" free-tagging vocabulary on the site.
236
      if (array_key_exists('field_tags', $tag_fields)) {
237 238 239
        $tag_field_name = 'field_tags';
      }
      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 248 249
        '#target_type' => 'taxonomy_term',
        '#selection_settings' => ['target_bundles' => $target_bundles],
        '#tags' => TRUE,
250 251
        '#size' => 30,
        '#maxlength' => 1024,
252
      ];
253 254 255
    }
  }

merlinofchaos's avatar
merlinofchaos committed
256
}