5 * Provide views runtime hooks for node.module.
8 use Drupal\user\RoleInterface;
9 use Drupal\views\ViewExecutable;
10 use Drupal\user\Entity\Role;
13 * Implements hook_views_query_substitutions().
15 function node_views_query_substitutions(ViewExecutable $view) {
16 $account = \Drupal::currentUser();
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')),
25 * Implements hook_views_analyze().
27 function node_views_analyze(ViewExecutable $view) {
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');
43 $filters = $display->getOption('filters');
44 foreach ($filters as $filter) {
45 if ($filter['table'] == 'node' && ($filter['field'] == 'status' || $filter['field'] == 'status_extra')) {
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');
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');