View source
<?php
namespace Drupal\og_menu\Plugin\Block;
use Drupal\Component\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\og\OgGroupAudienceHelperInterface;
use Drupal\og_menu\OgMenuInstanceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OgMenuBlock extends BlockBase implements ContainerFactoryPluginInterface, ContextAwarePluginInterface {
protected $menu_name;
protected $menuTree;
protected $menuActiveTrail;
protected $accessManager;
protected $account;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail, AccessManagerInterface $access_manager, AccountInterface $account, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->menuTree = $menu_tree;
$this->menuActiveTrail = $menu_active_trail;
$this->accessManager = $access_manager;
$this->account = $account;
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('menu.link_tree'), $container
->get('menu.active_trail'), $container
->get('access_manager'), $container
->get('current_user'), $container
->get('entity_type.manager'));
}
public function blockForm($form, FormStateInterface $form_state) {
$config = $this->configuration;
$defaults = $this
->defaultConfiguration();
$form['menu_levels'] = array(
'#type' => 'details',
'#title' => $this
->t('Menu levels'),
'#open' => $defaults['level'] !== $config['level'] || $defaults['depth'] !== $config['depth'],
'#process' => [
[
get_class(),
'processMenuLevelParents',
],
],
);
$options = range(0, $this->menuTree
->maxDepth());
unset($options[0]);
$form['menu_levels']['level'] = array(
'#type' => 'select',
'#title' => $this
->t('Initial menu level'),
'#default_value' => $config['level'],
'#options' => $options,
'#description' => $this
->t('The menu will only be visible if the menu item for the current page is at or below the selected starting level. Select level 1 to always keep this menu visible.'),
'#required' => TRUE,
);
$options[0] = $this
->t('Unlimited');
$form['menu_levels']['depth'] = array(
'#type' => 'select',
'#title' => $this
->t('Maximum number of menu levels to display'),
'#default_value' => $config['depth'],
'#options' => $options,
'#description' => $this
->t('The maximum number of menu levels to show, starting from the initial menu level. For example: with an initial level 2 and a maximum number of 3, menu levels 2, 3 and 4 can be displayed.'),
'#required' => TRUE,
);
return $form;
}
public static function processMenuLevelParents(&$element, FormStateInterface $form_state, &$complete_form) {
array_pop($element['#parents']);
return $element;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['level'] = $form_state
->getValue('level');
$this->configuration['depth'] = $form_state
->getValue('depth');
}
public function build() {
$menu_name = $this
->getMenuName();
$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 = array(
array(
'callable' => 'menu.default_tree_manipulators:checkAccess',
),
array(
'callable' => 'menu.default_tree_manipulators:generateIndexAndSort',
),
);
$tree = $this->menuTree
->transform($tree, $manipulators);
$build = $this->menuTree
->build($tree);
if (!$tree) {
$route_name = 'entity.ogmenu_instance.create';
$og_entity = $this
->getContext('og')
->getContextData()
->getValue();
$route_parameters = [
'ogmenu' => $this
->getDerivativeId(),
'og_group_entity_type' => $og_entity
->getEntityTypeId(),
'og_group' => $og_entity
->id(),
];
$access = $this->accessManager
->checkNamedRoute($route_name, $route_parameters, $this->account, TRUE);
$build['create'] = array(
'#theme' => 'menu_local_action',
'#link' => array(
'title' => $this
->t('Add menu'),
'url' => Url::fromRoute('entity.ogmenu_instance.create', $route_parameters),
),
'#access' => $access,
);
}
$menu_instance = $this
->getOgMenuInstance();
if ($menu_instance instanceof OgMenuInstanceInterface) {
$build['#contextual_links']['ogmenu'] = array(
'route_parameters' => array(
'ogmenu_instance' => $menu_instance
->id(),
),
);
}
if ($menu_instance) {
$menu_name = $menu_instance
->getType();
$build['#theme'] = 'menu__og__' . strtr($menu_name, '-', '_');
}
return $build;
}
public function defaultConfiguration() {
return [
'level' => 1,
'depth' => 0,
];
}
public function getCacheTags() {
$tags = parent::getCacheTags();
$menu_name = $this
->getMenuName();
if (!empty($menu_name)) {
$tags = Cache::mergeTags($tags, [
$menu_name,
]);
}
return $tags;
}
public function getCacheContexts() {
$tags = [
'route.menu_active_trails:ogmenu-' . $this
->getDerivativeId(),
'og_group_context',
];
return Cache::mergeContexts(parent::getCacheContexts(), $tags);
}
public function getOgMenuInstance() {
$entity = $this
->getContext('og')
->getContextData()
->getValue();
$entity_id = $entity
->id();
$instances = $this->entityTypeManager
->getStorage('ogmenu_instance')
->loadByProperties([
'type' => $this
->getDerivativeId(),
OgGroupAudienceHelperInterface::DEFAULT_FIELD => $entity_id,
]);
if ($instances) {
return array_pop($instances);
}
return NULL;
}
public function getMenuName() {
if (isset($this->menu_name)) {
return $this->menu_name;
}
$instance = $this
->getOgMenuInstance();
if ($instance) {
$this->menu_name = 'ogmenu-' . $instance
->id();
}
return $this->menu_name;
}
}