node.views.inc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /**
  3. * @file
  4. * Provide views data for node.module.
  5. */
  6. use Drupal\user\RoleInterface;
  7. use Drupal\views\ViewExecutable;
  8. use Drupal\user\Entity\Role;
  9. use Drupal\views\Analyzer;
  10. /**
  11. * Implements hook_views_analyze().
  12. */
  13. function node_views_analyze(ViewExecutable $view) {
  14. $ret = [];
  15. // Check for something other than the default display:
  16. if ($view->storage->get('base_table') == 'node') {
  17. foreach ($view->displayHandlers as $display) {
  18. if (!$display->isDefaulted('access') || !$display->isDefaulted('filters')) {
  19. // check for no access control
  20. $access = $display->getOption('access');
  21. if (empty($access['type']) || $access['type'] == 'none') {
  22. $anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
  23. $anonymous_has_access = $anonymous_role && $anonymous_role->hasPermission('access content');
  24. $authenticated_role = Role::load(RoleInterface::AUTHENTICATED_ID);
  25. $authenticated_has_access = $authenticated_role && $authenticated_role->hasPermission('access content');
  26. if (!$anonymous_has_access || !$authenticated_has_access) {
  27. $ret[] = Analyzer::formatMessage(t('Some roles lack permission to access content, but display %display has no access control.', ['%display' => $display->display['display_title']]), 'warning');
  28. }
  29. $filters = $display->getOption('filters');
  30. foreach ($filters as $filter) {
  31. if ($filter['table'] == 'node' && ($filter['field'] == 'status' || $filter['field'] == 'status_extra')) {
  32. continue 2;
  33. }
  34. }
  35. $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');
  36. }
  37. }
  38. }
  39. }
  40. foreach ($view->displayHandlers as $display) {
  41. if ($display->getPluginId() == 'page') {
  42. if ($display->getOption('path') == 'node/%') {
  43. $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 Layout Builder.', ['%display' => $display->display['display_title']]), 'warning');
  44. }
  45. }
  46. }
  47. return $ret;
  48. }