MicrositeMenu.php in Entity Reference Hierarchy 8.2
File
modules/entity_hierarchy_microsite/src/Plugin/Block/MicrositeMenu.php
View source
<?php
namespace Drupal\entity_hierarchy_microsite\Plugin\Block;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_hierarchy_microsite\Plugin\MicrositePluginTrait;
use Drupal\node\NodeInterface;
use Drupal\system\Plugin\Block\SystemMenuBlock;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MicrositeMenu extends SystemMenuBlock implements ContainerFactoryPluginInterface {
use MicrositePluginTrait {
create as traitCreate;
buildConfigurationForm as traitBuildConfigurationForm;
submitConfigurationForm as traitSubmitConfigurationForm;
defaultConfiguration as traitDefaultConfiguration;
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + $this
->traitDefaultConfiguration();
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return parent::buildConfigurationForm($form, $form_state) + $this
->traitBuildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
return parent::submitConfigurationForm($form, $form_state) + $this
->traitSubmitConfigurationForm($form, $form_state);
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
return $instance
->setChildOfMicrositeLookup($container
->get('entity_hierarchy_microsite.microsite_lookup'))
->setEntityFieldManager($container
->get('entity_field.manager'));
}
public function build() {
$cache = new CacheableMetadata();
if (!($node = $this
->getContextValue('node')) || !$node instanceof NodeInterface || !($microsites = $this->childOfMicrositeLookup
->findMicrositesForNodeAndField($node, $this->configuration['field']))) {
$build = [];
if ($node) {
$cache
->addCacheableDependency($node);
}
$cache
->applyTo($build);
return $build;
}
$microsite = reset($microsites);
$cache
->addCacheableDependency($node);
$cache
->addCacheableDependency($microsite);
if ($home = $microsite
->getHome()) {
$cache
->addCacheableDependency($home);
}
$menu_name = $this
->getDerivativeId();
if ($this->configuration['expand_all_items']) {
$parameters = new MenuTreeParameters();
$active_trail = $this->menuActiveTrail
->getActiveTrailIds($menu_name);
$parameters
->setActiveTrail($active_trail);
}
else {
$parameters = $this->menuTree
->getCurrentRouteMenuTreeParameters($menu_name);
}
if ($home) {
$parameters
->setRoot('entity_hierarchy_microsite:' . $home
->uuid());
}
$level = $this->configuration['level'];
$depth = $this->configuration['depth'];
$parameters
->setMinDepth($level);
if ($depth > 0) {
$parameters
->setMaxDepth(min($level + $depth - 1, $this->menuTree
->maxDepth()));
}
if ($level > 1) {
if (count($parameters->activeTrail) >= $level) {
$menu_trail_ids = array_reverse(array_values($parameters->activeTrail));
if ($depth > 0) {
$parameters
->setMaxDepth(min($level - 1 + $depth - 1, $this->menuTree
->maxDepth()));
}
}
else {
$build = [];
$cache
->applyTo($build);
return $build;
}
}
$tree = $this->menuTree
->load($menu_name, $parameters);
$manipulators = [
[
'callable' => 'menu.default_tree_manipulators:checkAccess',
],
[
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
],
];
$tree = $this->menuTree
->transform($tree, $manipulators);
$build = $this->menuTree
->build($tree);
$cache
->applyTo($build);
return $build;
}
public function getDerivativeId() {
return 'entity-hierarchy-microsite';
}
}