Node.php 11.7 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1
2
3
4
<?php

/**
 * @file
5
 * Definition of Drupal\node\Plugin\views\wizard\Node.
merlinofchaos's avatar
merlinofchaos committed
6
7
 */

8
namespace Drupal\node\Plugin\views\wizard;
9

10
use Drupal\views\Plugin\views\wizard\WizardPluginBase;
11
use Drupal\Component\Annotation\Plugin;
12
13
14
15
16
use Drupal\Core\Annotation\Translation;

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

merlinofchaos's avatar
merlinofchaos committed
18
19
/**
 * Tests creating node views with the wizard.
20
21
 *
 * @Plugin(
aspilicious's avatar
aspilicious committed
22
 *   id = "node",
23
 *   module = "node",
24
 *   base_table = "node",
25
 *   title = @Translation("Content")
26
 * )
merlinofchaos's avatar
merlinofchaos committed
27
 */
28

29
class Node extends WizardPluginBase {
merlinofchaos's avatar
merlinofchaos committed
30

31
32
33
  /**
   * Set the created column.
   */
34
  protected $createdColumn = 'node_field_data-created';
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

  /**
   * Set default values for the path field options.
   */
  protected $pathField = array(
    'id' => 'nid',
    'table' => 'node',
    'field' => 'nid',
    'exclude' => TRUE,
    'link_to_node' => FALSE,
    'alter' => array(
      'alter_text' => TRUE,
      'text' => 'node/[nid]'
    )
  );

  /**
   * Set default values for the filters.
   */
  protected $filters = array(
    'status' => array(
      'value' => TRUE,
57
      'table' => 'node_field_data',
58
59
60
61
62
63
64
65
66
67
68
69
      'field' => 'status'
    )
  );

  /**
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::getAvailableSorts().
   *
   * @return array
   */
  public function getAvailableSorts() {
    // You can't execute functions in properties, so override the method
    return array(
70
      'node_field_data-title:DESC' => t('Title')
71
72
73
    );
  }

74
  /**
75
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::rowStyleOptions().
76
   */
77
  protected function rowStyleOptions() {
merlinofchaos's avatar
merlinofchaos committed
78
79
80
81
82
83
84
85
86
    $options = array();
    $options['teasers'] = t('teasers');
    $options['full_posts'] = t('full posts');
    $options['titles'] = t('titles');
    $options['titles_linked'] = t('titles (linked)');
    $options['fields'] = t('fields');
    return $options;
  }

87
88
89
90
91
92
93
94
95
96
  /**
   * Adds the style options to the wizard form.
   *
   * @param array $form
   *   The full wizard form array.
   * @param array $form_state
   *   The current state of the wizard form.
   * @param string $type
   *   The display ID (e.g. 'page' or 'block').
   */
97
98
  protected function buildFormStyle(array &$form, array &$form_state, $type) {
    parent::buildFormStyle($form, $form_state, $type);
merlinofchaos's avatar
merlinofchaos committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    $style_form =& $form['displays'][$type]['options']['style'];
    // Some style plugins don't support row plugins so stop here if that's the
    // case.
    if (!isset($style_form['row_plugin']['#default_value'])) {
      return;
    }
    $row_plugin = $style_form['row_plugin']['#default_value'];
    switch ($row_plugin) {
      case 'full_posts':
      case 'teasers':
        $style_form['row_options']['links'] = array(
          '#type' => 'select',
          '#title_display' => 'invisible',
          '#title' => t('Should links be displayed below each node'),
          '#options' => array(
            1 => t('with links (allow users to add comments, etc.)'),
            0 => t('without links'),
          ),
          '#default_value' => 1,
        );
        $style_form['row_options']['comments'] = array(
          '#type' => 'select',
          '#title_display' => 'invisible',
          '#title' => t('Should comments be displayed below each node'),
          '#options' => array(
            1 => t('with comments'),
            0 => t('without comments'),
          ),
          '#default_value' => 0,
        );
        break;
    }
  }

  /**
134
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::default_display_options().
merlinofchaos's avatar
merlinofchaos committed
135
   */
136
137
  protected function default_display_options() {
    $display_options = parent::default_display_options();
merlinofchaos's avatar
merlinofchaos committed
138
139
140
141
142
143
144
145
146
147
148

    // Add permission-based access control.
    $display_options['access']['type'] = 'perm';

    // 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';
149
    $display_options['fields']['title']['table'] = 'node_field_data';
merlinofchaos's avatar
merlinofchaos committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    $display_options['fields']['title']['field'] = 'title';
    $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;
    $display_options['fields']['title']['link_to_node'] = 1;

    return $display_options;
  }

167
168
169
  /**
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::default_display_filters_user().
   */
170
  protected function default_display_filters_user(array $form, array &$form_state) {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
    $filters = parent::default_display_filters_user($form, $form_state);

    if (!empty($form_state['values']['show']['tagged_with']['tids'])) {
      $filters['tid'] = array(
        'id' => 'tid',
        'table' => 'taxonomy_index',
        'field' => 'tid',
        'value' => $form_state['values']['show']['tagged_with']['tids'],
        'vocabulary' => $form_state['values']['show']['tagged_with']['vocabulary'],
      );
      // If the user entered more than one valid term in the autocomplete
      // field, they probably intended both of them to be applied.
      if (count($form_state['values']['show']['tagged_with']['tids']) > 1) {
        $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;
  }

194
195
196
197
  /**
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::page_display_options().
   */
  protected function page_display_options(array $form, array &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
198
199
200
201
202
203
204
    $display_options = parent::page_display_options($form, $form_state);
    $row_plugin = isset($form_state['values']['page']['style']['row_plugin']) ? $form_state['values']['page']['style']['row_plugin'] : NULL;
    $row_options = isset($form_state['values']['page']['style']['row_options']) ? $form_state['values']['page']['style']['row_options'] : array();
    $this->display_options_row($display_options, $row_plugin, $row_options);
    return $display_options;
  }

205
206
207
208
  /**
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::page_display_options().
   */
  protected function block_display_options(array $form, array &$form_state) {
merlinofchaos's avatar
merlinofchaos committed
209
210
211
212
213
214
215
216
217
218
219
220
221
    $display_options = parent::block_display_options($form, $form_state);
    $row_plugin = isset($form_state['values']['block']['style']['row_plugin']) ? $form_state['values']['block']['style']['row_plugin'] : NULL;
    $row_options = isset($form_state['values']['block']['style']['row_options']) ? $form_state['values']['block']['style']['row_options'] : array();
    $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':
222
223
224
225
        $display_options['row']['type'] = 'node';
        $display_options['row']['options']['build_mode'] = 'full';
        $display_options['row']['options']['links'] = !empty($row_options['links']);
        $display_options['row']['options']['comments'] = !empty($row_options['comments']);
merlinofchaos's avatar
merlinofchaos committed
226
227
        break;
      case 'teasers':
228
229
230
231
        $display_options['row']['type'] = 'node';
        $display_options['row']['options']['build_mode'] = 'teaser';
        $display_options['row']['options']['links'] = !empty($row_options['links']);
        $display_options['row']['options']['comments'] = !empty($row_options['comments']);
merlinofchaos's avatar
merlinofchaos committed
232
233
        break;
      case 'titles_linked':
234
        $display_options['row']['type'] = 'fields';
merlinofchaos's avatar
merlinofchaos committed
235
236
237
        $display_options['field']['title']['link_to_node'] = 1;
        break;
      case 'titles':
238
        $display_options['row']['type'] = 'fields';
merlinofchaos's avatar
merlinofchaos committed
239
240
241
242
        $display_options['field']['title']['link_to_node'] = 0;
        break;
    }
  }
243

244
  /**
245
   * Overrides Drupal\views\Plugin\views\wizard\WizardPluginBase::buildFilters().
246
247
248
   *
   * Add some options for filter by taxonomy terms.
   */
249
250
  protected function buildFilters(&$form, &$form_state) {
    parent::buildFilters($form, $form_state);
251

252
    $selected_bundle = static::getSelected($form_state, array('show', 'type'), 'all', $form['displays']['show']['type']);
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271

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

    // We construct this filter using taxonomy_index.tid (which limits the
    // filtering to a specific vocabulary) rather than taxonomy_term_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.

    // 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.
272
    $bundles = array_keys(entity_get_bundles($this->entity_type));
273
274
275
276
277
278
279
280
    // 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)) {
      $bundles = array($selected_bundle);
    }
    $tag_fields = array();
    foreach ($bundles as $bundle) {
      foreach (field_info_instances($this->entity_type, $bundle) as $instance) {
281
        $widget = entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')->getComponent($instance['field_name']);
282
283
        // We define "tag-like" taxonomy fields as ones that use the
        // "Autocomplete term widget (tagging)" widget.
284
        if ($widget['type'] == 'taxonomy_autocomplete') {
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
          $tag_fields[] = $instance['field_name'];
        }
      }
    }
    $tag_fields = array_unique($tag_fields);
    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.
      if (in_array('field_tags', $tag_fields)) {
        $tag_field_name = 'field_tags';
      }
      else {
        $tag_field_name = reset($tag_fields);
      }
      // Add the autocomplete textfield to the wizard.
      $form['displays']['show']['tagged_with'] = array(
        '#type' => 'textfield',
        '#title' => t('tagged with'),
        '#autocomplete_path' => 'taxonomy/autocomplete/' . $tag_field_name,
        '#size' => 30,
        '#maxlength' => 1024,
        '#field_name' => $tag_field_name,
        '#element_validate' => array('views_ui_taxonomy_autocomplete_validate'),
      );
    }
  }

merlinofchaos's avatar
merlinofchaos committed
316
}