groupmenu-8.x-1.0-beta2/modules/groupmenu_block/src/Plugin/Block/GroupMenuBlock.php
modules/groupmenu_block/src/Plugin/Block/GroupMenuBlock.php
<?php
namespace Drupal\groupmenu_block\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupRelationship;
use Drupal\groupmenu\GroupMenuService;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a block for displaying group menus.
*
* @Block(
* id = "groupmenus",
* admin_label = @Translation("Group menus block"),
* category = @Translation("Group menu block"),
* context_definitions = {
* "group" = @ContextDefinition("entity:group", required = FALSE, label = @Translation("Group")),
* "node" = @ContextDefinition("entity:node", required = FALSE, label = @Translation("Node"))
* }
* )
*/
class GroupMenuBlock extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
/**
* The group menu service.
*
* @var \Drupal\groupmenu\GroupMenuService
*/
protected GroupMenuService $groupMenuService;
/**
* The menu link tree service.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected MenuLinkTreeInterface $menuTree;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, GroupMenuService $groupMenuService, MenuLinkTreeInterface $menu_tree, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->groupMenuService = $groupMenuService;
$this->menuTree = $menu_tree;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('groupmenu.menu'),
$container->get('menu.link_tree'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'max_depth' => 0,
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['max_depth'] = [
'#type' => 'select',
'#title' => $this->t('Max Depth'),
'#default_value' => $this->configuration['max_depth'],
'#weight' => 1,
'#options' => range(0, 10),
'#description' => $this->t('Set 0 for no limit.'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function setInPreview(bool $in_preview) : void {
$this->inPreview = $in_preview;
}
/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['max_depth'] = $form_state->getValue('max_depth');
}
/**
* Gets group menu names from group ID.
*
* @param \Drupal\group\Entity\GroupInterface $group
* The group you want to load menus for.
*
* @return \Drupal\system\MenuInterface[]
* An array of menu objects keyed by menu name.
*/
protected function getGroupMenus(GroupInterface $group) {
return $this->groupMenuService->loadUserGroupMenusByGroup('view', $group->id());
}
/**
* {@inheritdoc}
*/
public function build() {
// Get the associated group content for the current node.
$node = $this->getContextValue('node');
if ($node) {
$group_relationships = GroupRelationship::loadByEntity($node);
$menus = [];
// Get each group this node belongs to.
foreach ($group_relationships as $group_relationship) {
/** @var \Drupal\group\Entity\GroupRelationshipInterface $group_relationship */
$group = $group_relationship->getGroup();
// Make an array of menus to render.
$menus = array_merge($menus, $this->getGroupMenus($group));
}
}
else {
// Not on a node page, but try to get the group anyway.
$group = $this->getContextValue('group');
if ($group) {
$menus = $this->getGroupMenus($group);
}
else {
return [];
}
}
// Render the menus.
$build = [];
$parameters = new MenuTreeParameters();
// Setting max depth.
if ($max_depth = $this->configuration['max_depth']) {
$parameters->setMaxDepth($max_depth);
}
$parameters->onlyEnabledLinks();
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
foreach ($menus as $menu) {
$tree = $this->menuTree->load($menu->id(), $parameters);
$tree = $this->menuTree->transform($tree, $manipulators);
$build[] = $this->menuTree->build($tree);
}
return $build;
}
}
