DomainMenuAccessMenuBlock.php in Domain Menu Access 8
File
src/Plugin/Block/DomainMenuAccessMenuBlock.php
View source
<?php
namespace Drupal\domain_menu_access\Plugin\Block;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\system\Plugin\Block\SystemMenuBlock;
class DomainMenuAccessMenuBlock extends SystemMenuBlock implements ContainerFactoryPluginInterface {
public function build() {
$menu_name = $this
->getDerivativeId();
if (!$this
->isDomainRestricted($menu_name)) {
return parent::build();
}
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($menu_name);
$level = $this->configuration['level'];
$depth = $this->configuration['depth'];
$parameters
->setMinDepth($level);
if ($depth > 0) {
$parameters
->setMaxDepth(min($level + $depth - 1, $this->menuTree
->maxDepth()));
}
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'domain_menu_access.default_tree_manipulators:checkDomain',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
return $this->menuTree
->build($tree);
}
protected function isDomainRestricted($menu_name) {
$config = \Drupal::config('domain_menu_access.settings')
->get('menu_enabled');
return in_array($menu_name, $config);
}
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), [
'url.site',
]);
}
}