public function DefaultMenuLinkTreeManipulators::checkNodeAccess

public DefaultMenuLinkTreeManipulators::checkNodeAccess(array $tree)

Performs access checking for nodes in an optimized way.

This manipulator should be added before the generic ::checkAccess() one, because it provides a performance optimization for ::checkAccess().

Parameters

\Drupal\Core\Menu\MenuLinkTreeElement[] $tree: The menu link tree to manipulate.

Return value

\Drupal\Core\Menu\MenuLinkTreeElement[] The manipulated menu link tree.

File

core/lib/Drupal/Core/Menu/DefaultMenuLinkTreeManipulators.php, line 131

Class

DefaultMenuLinkTreeManipulators
Provides a couple of menu link tree manipulators.

Namespace

Drupal\Core\Menu

Code

public function checkNodeAccess(array $tree) {
  $node_links = array();
  $this->collectNodeLinks($tree, $node_links);
  if ($node_links) {
    $nids = array_keys($node_links);

    $query = $this->queryFactory->get('node');
    $query->condition('nid', $nids, 'IN');

    // Allows admins to view all nodes, by both disabling node_access
    // query rewrite as well as not checking for the node status. The
    // 'view own unpublished nodes' permission is ignored to not require cache
    // entries per user.
    $access_result = AccessResult::allowed()->cachePerPermissions();
    if ($this->account->hasPermission('bypass node access')) {
      $query->accessCheck(FALSE);
    }
    else {
      $access_result->addCacheContexts(['user.node_grants:view']);
      $query->condition('status', NODE_PUBLISHED);
    }

    $nids = $query->execute();
    foreach ($nids as $nid) {
      foreach ($node_links[$nid] as $key => $link) {
        $node_links[$nid][$key]->access = $access_result;
      }
    }
  }

  return $tree;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Menu!DefaultMenuLinkTreeManipulators.php/function/DefaultMenuLinkTreeManipulators::checkNodeAccess/8.1.x