You are here

public function DomainMenuLinkTreeManipulators::checkDomain in Domain Menu Access 8

Performs access checking for menu link content in an optimized way.

This manipulator should be added after the generic ::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

src/Menu/DomainMenuLinkTreeManipulators.php, line 74

Class

DomainMenuLinkTreeManipulators
Provides a couple of menu link tree manipulators.

Namespace

Drupal\domain_menu_access\Menu

Code

public function checkDomain(array $tree) {
  $current_language = $this->languageManager
    ->getCurrentLanguage();
  foreach ($tree as $key => $element) {

    // Other menu tree manipulators may already have calculated access, do not
    // overwrite the existing value in that case if already forbidden.
    if (!isset($element->access) || $tree[$key]->access
      ->isAllowed()) {
      if ($access = $this
        ->menuLinkCheckAccess($element->link, $current_language)) {
        if ($access
          ->isForbidden()) {
          $tree[$key]->access = $access;

          // Secure unavailable menu link.
          $tree[$key]->link = new InaccessibleMenuLink($tree[$key]->link);
          $tree[$key]->subtree = [];
        }
        elseif ($tree[$key]->subtree) {
          $tree[$key]->subtree = $this
            ->checkDomain($tree[$key]->subtree);
        }
      }
    }
    if (isset($tree[$key]->access)) {
      $tree[$key]->access
        ->addCacheContexts([
        'url.site',
      ]);
    }
  }
  return $tree;
}