Issue #1479454 by Hugo Wetterberg, galooph, dawehner, andypost, marcingy, heyrocker...
[project/drupal.git] / core / modules / node / node.views.inc
1 <?php
2
3 /**
4 * @file
5 * Provide views data and handlers for node.module.
6 *
7 * @ingroup views_module_handlers
8 */
9
10 use Drupal\views\Analyzer;
11 use Drupal\views\ViewExecutable;
12
13 /**
14 * Implements hook_views_data().
15 */
16 function node_views_data() {
17 // node table -- basic table information.
18
19 // Define the base group of this table. Fields that don't
20 // have a group defined will go into this field by default.
21 $data['node']['table']['group'] = t('Content');
22
23 // Advertise this table as a possible base table
24 $data['node']['table']['base'] = array(
25 'field' => 'nid',
26 'title' => t('Content'),
27 'weight' => -10,
28 'access query tag' => 'node_access',
29 'defaults' => array(
30 'field' => 'title',
31 ),
32 );
33 $data['node']['table']['entity type'] = 'node';
34 $data['node']['table']['wizard_id'] = 'node';
35
36 // node table -- fields
37
38 // nid
39 $data['node']['nid'] = array(
40 'title' => t('Nid'),
41 'help' => t('The node ID.'), // The help that appears on the UI,
42 // Information for displaying the nid
43 'field' => array(
44 'id' => 'node',
45 'click sortable' => TRUE,
46 ),
47 // Information for accepting a nid as an argument
48 'argument' => array(
49 'id' => 'node_nid',
50 'name field' => 'title', // the field to display in the summary.
51 'numeric' => TRUE,
52 'validate type' => 'nid',
53 ),
54 // Information for accepting a nid as a filter
55 'filter' => array(
56 'id' => 'numeric',
57 ),
58 // Information for sorting on a nid.
59 'sort' => array(
60 'id' => 'standard',
61 ),
62 );
63
64 // title
65 // This definition has more items in it than it needs to as an example.
66 $data['node']['title'] = array(
67 'title' => t('Title'), // The item it appears as on the UI,
68 'help' => t('The content title.'), // The help that appears on the UI,
69 // Information for displaying a title as a field
70 'field' => array(
71 'field' => 'title', // the real field. This could be left out since it is the same.
72 'group' => t('Content'), // The group it appears in on the UI. Could be left out.
73 'id' => 'node',
74 'click sortable' => TRUE,
75 'link_to_node default' => TRUE,
76 ),
77 'sort' => array(
78 'id' => 'standard',
79 ),
80 // Information for accepting a title as a filter
81 'filter' => array(
82 'id' => 'string',
83 ),
84 'argument' => array(
85 'id' => 'string',
86 ),
87 );
88
89 // created field
90 $data['node']['created'] = array(
91 'title' => t('Post date'), // The item it appears as on the UI,
92 'help' => t('The date the content was posted.'), // The help that appears on the UI,
93 'field' => array(
94 'id' => 'date',
95 'click sortable' => TRUE,
96 ),
97 'sort' => array(
98 'id' => 'date'
99 ),
100 'filter' => array(
101 'id' => 'date',
102 ),
103 );
104
105 // changed field
106 $data['node']['changed'] = array(
107 'title' => t('Updated date'), // The item it appears as on the UI,
108 'help' => t('The date the content was last updated.'), // The help that appears on the UI,
109 'field' => array(
110 'id' => 'date',
111 'click sortable' => TRUE,
112 ),
113 'sort' => array(
114 'id' => 'date'
115 ),
116 'filter' => array(
117 'id' => 'date',
118 ),
119 );
120
121 // Content type
122 $data['node']['type'] = array(
123 'title' => t('Type'), // The item it appears as on the UI,
124 'help' => t('The content type (for example, "blog entry", "forum post", "story", etc).'), // The help that appears on the UI,
125 'field' => array(
126 'id' => 'node_type',
127 'click sortable' => TRUE,
128 ),
129 'sort' => array(
130 'id' => 'standard',
131 ),
132 'filter' => array(
133 'id' => 'node_type',
134 ),
135 'argument' => array(
136 'id' => 'node_type',
137 ),
138 );
139
140 // published status
141 $data['node']['status'] = array(
142 'title' => t('Published'),
143 'help' => t('Whether or not the content is published.'),
144 'field' => array(
145 'id' => 'boolean',
146 'click sortable' => TRUE,
147 'output formats' => array(
148 'published-notpublished' => array(t('Published'), t('Not published')),
149 ),
150 ),
151 'filter' => array(
152 'id' => 'boolean',
153 'label' => t('Published'),
154 'type' => 'yes-no',
155 'use_equal' => TRUE, // Use status = 1 instead of status <> 0 in WHERE statment
156 ),
157 'sort' => array(
158 'id' => 'standard',
159 ),
160 );
161
162 // published status + extra
163 $data['node']['status_extra'] = array(
164 'title' => t('Published or admin'),
165 'help' => t('Filters out unpublished content if the current user cannot view it.'),
166 'filter' => array(
167 'field' => 'status',
168 'id' => 'node_status',
169 'label' => t('Published or admin'),
170 ),
171 );
172
173 // promote status
174 $data['node']['promote'] = array(
175 'title' => t('Promoted to front page'),
176 'help' => t('Whether or not the content is promoted to the front page.'),
177 'field' => array(
178 'id' => 'boolean',
179 'click sortable' => TRUE,
180 'output formats' => array(
181 'promoted-notpromoted' => array(t('Promoted'), t('Not promoted')),
182 ),
183 ),
184 'filter' => array(
185 'id' => 'boolean',
186 'label' => t('Promoted to front page'),
187 'type' => 'yes-no',
188 ),
189 'sort' => array(
190 'id' => 'standard',
191 ),
192 );
193
194 // sticky
195 $data['node']['sticky'] = array(
196 'title' => t('Sticky'), // The item it appears as on the UI,
197 'help' => t('Whether or not the content is sticky.'), // The help that appears on the UI,
198 // Information for displaying a title as a field
199 'field' => array(
200 'id' => 'boolean',
201 'click sortable' => TRUE,
202 'output formats' => array(
203 'sticky' => array(t('Sticky'), t('Not sticky')),
204 ),
205 ),
206 'filter' => array(
207 'id' => 'boolean',
208 'label' => t('Sticky'),
209 'type' => 'yes-no',
210 ),
211 'sort' => array(
212 'id' => 'standard',
213 'help' => t('Whether or not the content is sticky. To list sticky content first, set this to descending.'),
214 ),
215 );
216
217 // Language field
218 if (module_exists('language')) {
219 $data['node']['langcode'] = array(
220 'title' => t('Language'),
221 'help' => t('The language the content is in.'),
222 'field' => array(
223 'id' => 'node_language',
224 'click sortable' => TRUE,
225 ),
226 'filter' => array(
227 'id' => 'language',
228 ),
229 'argument' => array(
230 'id' => 'language',
231 ),
232 'sort' => array(
233 'id' => 'standard',
234 ),
235 );
236 }
237
238 // Define some fields based upon views_handler_field_entity in the entity
239 // table so they can be re-used with other query backends.
240 // @see views_handler_field_entity
241
242 $data['views_entity_node']['table']['group'] = t('Content');
243
244 $data['views_entity_node']['view_node'] = array(
245 'field' => array(
246 'title' => t('Link'),
247 'help' => t('Provide a simple link to the content.'),
248 'id' => 'node_link',
249 ),
250 );
251
252 $data['views_entity_node']['edit_node'] = array(
253 'field' => array(
254 'title' => t('Edit link'),
255 'help' => t('Provide a simple link to edit the content.'),
256 'id' => 'node_link_edit',
257 ),
258 );
259
260 $data['views_entity_node']['delete_node'] = array(
261 'field' => array(
262 'title' => t('Delete link'),
263 'help' => t('Provide a simple link to delete the content.'),
264 'id' => 'node_link_delete',
265 ),
266 );
267
268 $data['node']['path'] = array(
269 'field' => array(
270 'title' => t('Path'),
271 'help' => t('The aliased path to this content.'),
272 'id' => 'node_path',
273 ),
274 );
275
276 // Bogus fields for aliasing purposes.
277
278 $data['node']['created_fulldate'] = array(
279 'title' => t('Created date'),
280 'help' => t('Date in the form of CCYYMMDD.'),
281 'argument' => array(
282 'field' => 'created',
283 'id' => 'node_created_fulldate',
284 ),
285 );
286
287 $data['node']['created_year_month'] = array(
288 'title' => t('Created year + month'),
289 'help' => t('Date in the form of YYYYMM.'),
290 'argument' => array(
291 'field' => 'created',
292 'id' => 'node_created_year_month',
293 ),
294 );
295
296 $data['node']['created_year'] = array(
297 'title' => t('Created year'),
298 'help' => t('Date in the form of YYYY.'),
299 'argument' => array(
300 'field' => 'created',
301 'id' => 'node_created_year',
302 ),
303 );
304
305 $data['node']['created_month'] = array(
306 'title' => t('Created month'),
307 'help' => t('Date in the form of MM (01 - 12).'),
308 'argument' => array(
309 'field' => 'created',
310 'id' => 'node_created_month',
311 ),
312 );
313
314 $data['node']['created_day'] = array(
315 'title' => t('Created day'),
316 'help' => t('Date in the form of DD (01 - 31).'),
317 'argument' => array(
318 'field' => 'created',
319 'id' => 'node_created_day',
320 ),
321 );
322
323 $data['node']['created_week'] = array(
324 'title' => t('Created week'),
325 'help' => t('Date in the form of WW (01 - 53).'),
326 'argument' => array(
327 'field' => 'created',
328 'id' => 'node_created_week',
329 ),
330 );
331
332 $data['node']['changed_fulldate'] = array(
333 'title' => t('Updated date'),
334 'help' => t('Date in the form of CCYYMMDD.'),
335 'argument' => array(
336 'field' => 'changed',
337 'id' => 'node_created_fulldate',
338 ),
339 );
340
341 $data['node']['changed_year_month'] = array(
342 'title' => t('Updated year + month'),
343 'help' => t('Date in the form of YYYYMM.'),
344 'argument' => array(
345 'field' => 'changed',
346 'id' => 'node_created_year_month',
347 ),
348 );
349
350 $data['node']['changed_year'] = array(
351 'title' => t('Updated year'),
352 'help' => t('Date in the form of YYYY.'),
353 'argument' => array(
354 'field' => 'changed',
355 'id' => 'node_created_year',
356 ),
357 );
358
359 $data['node']['changed_month'] = array(
360 'title' => t('Updated month'),
361 'help' => t('Date in the form of MM (01 - 12).'),
362 'argument' => array(
363 'field' => 'changed',
364 'id' => 'node_created_month',
365 ),
366 );
367
368 $data['node']['changed_day'] = array(
369 'title' => t('Updated day'),
370 'help' => t('Date in the form of DD (01 - 31).'),
371 'argument' => array(
372 'field' => 'changed',
373 'id' => 'node_created_day',
374 ),
375 );
376
377 $data['node']['changed_week'] = array(
378 'title' => t('Updated week'),
379 'help' => t('Date in the form of WW (01 - 53).'),
380 'argument' => array(
381 'field' => 'changed',
382 'id' => 'node_created_week',
383 ),
384 );
385
386 // uid field
387 $data['node']['uid'] = array(
388 'title' => t('Author uid'),
389 'help' => t('The user authoring the content. If you need more fields than the uid add the content: author relationship'),
390 'relationship' => array(
391 'title' => t('Author'),
392 'help' => t('Relate content to the user who created it.'),
393 'id' => 'standard',
394 'base' => 'users',
395 'field' => 'uid',
396 'label' => t('author'),
397 ),
398 'filter' => array(
399 'id' => 'user_name',
400 ),
401 'argument' => array(
402 'id' => 'numeric',
403 ),
404 'field' => array(
405 'id' => 'user',
406 ),
407 );
408
409 $data['node']['uid_revision'] = array(
410 'title' => t('User has a revision'),
411 'help' => t('All nodes where a certain user has a revision'),
412 'real field' => 'nid',
413 'filter' => array(
414 'id' => 'node_uid_revision',
415 ),
416 'argument' => array(
417 'id' => 'node_uid_revision',
418 ),
419 );
420
421 // Content revision table
422
423 // Define the base group of this table. Fields that don't
424 // have a group defined will go into this field by default.
425 $data['node_revision']['table']['entity type'] = 'node';
426 $data['node_revision']['table']['group'] = t('Content revision');
427 $data['node_revision']['table']['wizard_id'] = 'node_revision';
428
429
430 // Advertise this table as a possible base table
431 $data['node_revision']['table']['base'] = array(
432 'field' => 'vid',
433 'title' => t('Content revision'),
434 'help' => t('Content revision is a history of changes to content.'),
435 'defaults' => array(
436 'field' => 'title',
437 ),
438 );
439
440 // For other base tables, explain how we join
441 $data['node_revision']['table']['join'] = array(
442 // Directly links to node table.
443 'node' => array(
444 'left_field' => 'vid',
445 'field' => 'vid',
446 ),
447 );
448
449 // uid field for node revision
450 $data['node_revision']['uid'] = array(
451 'title' => t('User'),
452 'help' => t('Relate a content revision to the user who created the revision.'),
453 'relationship' => array(
454 'id' => 'standard',
455 'base' => 'users',
456 'base field' => 'uid',
457 'label' => t('revision user'),
458 ),
459 );
460
461 // nid
462 $data['node_revision']['nid'] = array(
463 'title' => t('Nid'),
464 // The help that appears on the UI.
465 'help' => t('The revision NID of the content revision.'),
466 // Information for displaying the nid.
467 'field' => array(
468 'id' => 'standard',
469 'click sortable' => TRUE,
470 ),
471 // Information for accepting a nid as an argument.
472 'argument' => array(
473 'id' => 'node_nid',
474 'click sortable' => TRUE,
475 'numeric' => TRUE,
476 ),
477 // Information for accepting a nid as a filter.
478 'filter' => array(
479 'id' => 'numeric',
480 ),
481 // Information for sorting on a nid.
482 'sort' => array(
483 'id' => 'standard',
484 ),
485 'relationship' => array(
486 'id' => 'standard',
487 'base' => 'node',
488 'base field' => 'nid',
489 'title' => t('Content'),
490 'label' => t('Get the actual content from a content revision.'),
491 ),
492 );
493
494 // vid
495 $data['node_revision']['vid'] = array(
496 'title' => t('Vid'),
497 'help' => t('The revision ID of the content revision.'),
498 // Information for displaying the vid
499 'field' => array(
500 'id' => 'standard',
501 'click sortable' => TRUE,
502 ),
503 // Information for accepting a vid as an argument
504 'argument' => array(
505 'id' => 'node_vid',
506 'click sortable' => TRUE,
507 'numeric' => TRUE,
508 ),
509 // Information for accepting a vid as a filter
510 'filter' => array(
511 'id' => 'numeric',
512 ),
513 // Information for sorting on a vid.
514 'sort' => array(
515 'id' => 'standard',
516 ),
517 'relationship' => array(
518 'id' => 'standard',
519 'base' => 'node',
520 'base field' => 'vid',
521 'title' => t('Content'),
522 'label' => t('Get the actual content from a content revision.'),
523 ),
524 );
525
526 // published status
527 $data['node_revision']['status'] = array(
528 'title' => t('Published'),
529 'help' => t('Whether or not the content is published.'),
530 'field' => array(
531 'id' => 'boolean',
532 'click sortable' => TRUE,
533 'output formats' => array(
534 'published-notpublished' => array(t('Published'), t('Not published')),
535 ),
536 ),
537 'filter' => array(
538 'id' => 'boolean',
539 'label' => t('Published'),
540 'type' => 'yes-no',
541 'use_equal' => TRUE, // Use status = 1 instead of status <> 0 in WHERE statment
542 ),
543 'sort' => array(
544 'id' => 'standard',
545 ),
546 );
547
548 // title
549 $data['node_revision']['title'] = array(
550 'title' => t('Title'), // The item it appears as on the UI,
551 'help' => t('The content title.'), // The help that appears on the UI,
552 // Information for displaying a title as a field
553 'field' => array(
554 'field' => 'title', // the real field
555 'id' => 'node_revision',
556 'click sortable' => TRUE,
557 ),
558 'sort' => array(
559 'id' => 'standard',
560 ),
561 'filter' => array(
562 'id' => 'string',
563 ),
564 'argument' => array(
565 'id' => 'string',
566 ),
567 );
568
569 // log field
570 $data['node_revision']['log'] = array(
571 'title' => t('Log message'), // The item it appears as on the UI,
572 'help' => t('The log message entered when the revision was created.'), // The help that appears on the UI,
573 // Information for displaying a title as a field
574 'field' => array(
575 'id' => 'xss',
576 ),
577 'filter' => array(
578 'id' => 'string',
579 ),
580 );
581
582 // revision timestamp
583 // changed field
584 $data['node_revision']['timestamp'] = array(
585 'title' => t('Updated date'), // The item it appears as on the UI,
586 'help' => t('The date the node was last updated.'), // The help that appears on the UI,
587 'field' => array(
588 'id' => 'date',
589 'click sortable' => TRUE,
590 ),
591 'sort' => array(
592 'id' => 'date'
593 ),
594 'filter' => array(
595 'id' => 'date',
596 ),
597 );
598
599 $data['node_revision']['link_to_revision'] = array(
600 'field' => array(
601 'title' => t('Link'),
602 'help' => t('Provide a simple link to the revision.'),
603 'id' => 'node_revision_link',
604 ),
605 );
606
607 $data['node_revision']['revert_revision'] = array(
608 'field' => array(
609 'title' => t('Revert link'),
610 'help' => t('Provide a simple link to revert to the revision.'),
611 'id' => 'node_revision_link_revert',
612 ),
613 );
614
615 $data['node_revision']['delete_revision'] = array(
616 'field' => array(
617 'title' => t('Delete link'),
618 'help' => t('Provide a simple link to delete the content revision.'),
619 'id' => 'node_revision_link_delete',
620 ),
621 );
622
623 // Node access table
624
625 // Define the base group of this table. Fields that don't
626 // have a group defined will go into this field by default.
627 $data['node_access']['table']['group'] = t('Content access');
628
629 // For other base tables, explain how we join
630 $data['node_access']['table']['join'] = array(
631 // Directly links to node table.
632 'node' => array(
633 'left_field' => 'nid',
634 'field' => 'nid',
635 ),
636 );
637 // nid field
638 $data['node_access']['nid'] = array(
639 'title' => t('Access'),
640 'help' => t('Filter by access.'),
641 'filter' => array(
642 'id' => 'node_access',
643 'help' => t('Filter for content by view access. <strong>Not necessary if you are using node as your base table.</strong>'),
644 ),
645 );
646
647 return $data;
648 }
649
650 /**
651 * Implements hook_preprocess_node().
652 */
653 function node_row_node_view_preprocess_node(&$vars) {
654 $node = $vars['node'];
655 $options = $vars['view']->style_plugin->row_plugin->options;
656
657 // Prevent the comment form from showing up if this is not a page display.
658 if ($vars['view_mode'] == 'full' && !$vars['view']->display_handler->hasPath()) {
659 $node->comment = FALSE;
660 }
661
662 if (!$options['links']) {
663 unset($vars['content']['links']);
664 }
665
666 if (!empty($options['comments']) && user_access('access comments') && $node->comment) {
667 $vars['content']['comments'] = comment_node_page_additions($node);
668 }
669 }
670
671 /**
672 * Implements hook_views_query_substitutions().
673 */
674 function node_views_query_substitutions() {
675 return array(
676 '***ADMINISTER_NODES***' => intval(user_access('administer nodes')),
677 '***VIEW_OWN_UNPUBLISHED_NODES***' => intval(user_access('view own unpublished content')),
678 '***BYPASS_NODE_ACCESS***' => intval(user_access('bypass node access')),
679 );
680 }
681
682 /**
683 * Implements hook_views_analyze().
684 */
685 function node_views_analyze(ViewExecutable $view) {
686 $ret = array();
687 // Check for something other than the default display:
688 if ($view->storage->get('base_table') == 'node') {
689 foreach ($view->displayHandlers as $id => $display) {
690 if (!$display->isDefaulted('access') || !$display->isDefaulted('filters')) {
691 // check for no access control
692 $access = $display->getOption('access');
693 if (empty($access['type']) || $access['type'] == 'none') {
694 $result = db_select('role_permission', 'p')
695 ->fields('p', array('rid', 'permission'))
696 ->condition('p.rid', array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID), 'IN')
697 ->condition('p.permission', 'access content')
698 ->execute();
699
700 foreach ($result as $role) {
701 $role->safe = TRUE;
702 $roles[$role->rid] = $role;
703 }
704 if (!($roles[DRUPAL_ANONYMOUS_RID]->safe && $roles[DRUPAL_AUTHENTICATED_RID]->safe)) {
705 $ret[] = Analyzer::formatMessage(t('Some roles lack permission to access content, but display %display has no access control.', array('%display' => $display->display['display_title'])), 'warning');
706 }
707 $filters = $display->getOption('filters');
708 foreach ($filters as $filter) {
709 if ($filter['table'] == 'node' && ($filter['field'] == 'status' || $filter['field'] == 'status_extra')) {
710 continue 2;
711 }
712 }
713 $ret[] = Analyzer::formatMessage(t('Display %display has no access control but does not contain a filter for published nodes.', array('%display' => $display->display['display_title'])), 'warning');
714 }
715 }
716 }
717 }
718 foreach ($view->displayHandlers as $display) {
719 if ($display->getPluginId() == 'page') {
720 if ($display->getOption('path') == 'node/%') {
721 $ret[] = Analyzer::formatMessage(t('Display %display has set node/% as path. This will not produce what you want. If you want to have multiple versions of the node view, use panels.', array('%display' => $display->display['display_title'])), 'warning');
722 }
723 }
724 }
725
726 return $ret;
727 }
728
729 /**
730 * Implements hook_views_wizard().
731 */
732 function node_views_wizard() {
733 // @todo: figure this piece out.
734 if (module_exists('statistics')) {
735 $plugins['node']['available_sorts']['node_counter-totalcount:DESC'] = t('Number of hits');
736 }
737
738 }