2015-08-18 00:00:26 +00:00
< ? php
/**
* @ file
* Provide views runtime hooks for node . module .
*/
use Drupal\user\RoleInterface ;
use Drupal\views\ViewExecutable ;
use Drupal\user\Entity\Role ;
/**
* Implements hook_views_query_substitutions () .
*/
function node_views_query_substitutions ( ViewExecutable $view ) {
$account = \Drupal :: currentUser ();
2017-04-13 14:53:35 +00:00
return [
2015-08-18 00:00:26 +00:00
'***ADMINISTER_NODES***' => intval ( $account -> hasPermission ( 'administer nodes' )),
'***VIEW_OWN_UNPUBLISHED_NODES***' => intval ( $account -> hasPermission ( 'view own unpublished content' )),
2016-05-04 21:35:41 +00:00
'***BYPASS_NODE_ACCESS***' => intval ( $account -> hasPermission ( 'bypass node access' )),
2017-04-13 14:53:35 +00:00
];
2015-08-18 00:00:26 +00:00
}
/**
* Implements hook_views_analyze () .
*/
function node_views_analyze ( ViewExecutable $view ) {
2017-04-13 14:53:35 +00:00
$ret = [];
2015-08-18 00:00:26 +00:00
// Check for something other than the default display:
if ( $view -> storage -> get ( 'base_table' ) == 'node' ) {
foreach ( $view -> displayHandlers as $display ) {
if ( ! $display -> isDefaulted ( 'access' ) || ! $display -> isDefaulted ( 'filters' )) {
// check for no access control
$access = $display -> getOption ( 'access' );
if ( empty ( $access [ 'type' ]) || $access [ 'type' ] == 'none' ) {
$anonymous_role = Role :: load ( RoleInterface :: ANONYMOUS_ID );
$anonymous_has_access = $anonymous_role && $anonymous_role -> hasPermission ( 'access content' );
$authenticated_role = Role :: load ( RoleInterface :: AUTHENTICATED_ID );
$authenticated_has_access = $authenticated_role && $authenticated_role -> hasPermission ( 'access content' );
if ( ! $anonymous_has_access || ! $authenticated_has_access ) {
2017-04-13 14:53:35 +00:00
$ret [] = Analyzer :: formatMessage ( t ( 'Some roles lack permission to access content, but display %display has no access control.' , [ '%display' => $display -> display [ 'display_title' ]]), 'warning' );
2015-08-18 00:00:26 +00:00
}
$filters = $display -> getOption ( 'filters' );
foreach ( $filters as $filter ) {
if ( $filter [ 'table' ] == 'node' && ( $filter [ 'field' ] == 'status' || $filter [ 'field' ] == 'status_extra' )) {
continue 2 ;
}
}
2017-04-13 14:53:35 +00:00
$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' );
2015-08-18 00:00:26 +00:00
}
}
}
}
foreach ( $view -> displayHandlers as $display ) {
if ( $display -> getPluginId () == 'page' ) {
if ( $display -> getOption ( 'path' ) == 'node/%' ) {
2017-04-13 14:53:35 +00:00
$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' );
2015-08-18 00:00:26 +00:00
}
}
}
return $ret ;
}