Version 1
[yaffs-website] / web / core / modules / node / node.views_execution.inc
1 <?php
2
3 /**
4  * @file
5  * Provide views runtime hooks for node.module.
6  */
7
8 use Drupal\user\RoleInterface;
9 use Drupal\views\ViewExecutable;
10 use Drupal\user\Entity\Role;
11
12 /**
13  * Implements hook_views_query_substitutions().
14  */
15 function node_views_query_substitutions(ViewExecutable $view) {
16   $account = \Drupal::currentUser();
17   return [
18     '***ADMINISTER_NODES***' => intval($account->hasPermission('administer nodes')),
19     '***VIEW_OWN_UNPUBLISHED_NODES***' => intval($account->hasPermission('view own unpublished content')),
20     '***BYPASS_NODE_ACCESS***' => intval($account->hasPermission('bypass node access')),
21   ];
22 }
23
24 /**
25  * Implements hook_views_analyze().
26  */
27 function node_views_analyze(ViewExecutable $view) {
28   $ret = [];
29   // Check for something other than the default display:
30   if ($view->storage->get('base_table') == 'node') {
31     foreach ($view->displayHandlers as $display) {
32       if (!$display->isDefaulted('access') || !$display->isDefaulted('filters')) {
33         // check for no access control
34         $access = $display->getOption('access');
35         if (empty($access['type']) || $access['type'] == 'none') {
36           $anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
37           $anonymous_has_access = $anonymous_role && $anonymous_role->hasPermission('access content');
38           $authenticated_role = Role::load(RoleInterface::AUTHENTICATED_ID);
39           $authenticated_has_access = $authenticated_role && $authenticated_role->hasPermission('access content');
40           if (!$anonymous_has_access || !$authenticated_has_access) {
41             $ret[] = Analyzer::formatMessage(t('Some roles lack permission to access content, but display %display has no access control.', ['%display' => $display->display['display_title']]), 'warning');
42           }
43           $filters = $display->getOption('filters');
44           foreach ($filters as $filter) {
45             if ($filter['table'] == 'node' && ($filter['field'] == 'status' || $filter['field'] == 'status_extra')) {
46               continue 2;
47             }
48           }
49           $ret[] = Analyzer::formatMessage(t('Display %display has no access control but does not contain a filter for published nodes.', ['%display' => $display->display['display_title']]), 'warning');
50         }
51       }
52     }
53   }
54   foreach ($view->displayHandlers as $display) {
55     if ($display->getPluginId() == 'page') {
56       if ($display->getOption('path') == 'node/%') {
57         $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.', ['%display' => $display->display['display_title']]), 'warning');
58       }
59     }
60   }
61
62   return $ret;
63 }