diff --git a/src/Plugin/Block/GroupMenuBlock.php b/src/Plugin/Block/GroupMenuBlock.php index 6ef2a9a..e3872ca 100644 --- a/src/Plugin/Block/GroupMenuBlock.php +++ b/src/Plugin/Block/GroupMenuBlock.php @@ -12,6 +12,9 @@ use Drupal\Core\Menu\MenuTreeParameters; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\group_content_menu\GroupContentMenuInterface; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\node\NodeInterface; +use Drupal\group\Entity\Group; +use Drupal\group\Entity\GroupRelationship; +use Drupal\group\Entity\GroupContent; /** * Provides a generic Menu block. @@ -191,20 +194,48 @@ class GroupMenuBlock extends BlockBase implements ContainerFactoryPluginInterfac } $tree = $this->menuTree->load($menu_name, $parameters); - $tree = $this->menuTree->transform($tree, $this->getMenuManipulators()); + $manipulators = [ + ['callable' => 'menu.default_tree_manipulators:checkAccess'], + ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], + ]; + $tree = $this->menuTree->transform($tree, $manipulators); $build = $this->menuTree->build($tree); $menu_instance = $this->getMenuInstance(); if ($menu_instance instanceof GroupContentMenuInterface) { - $build['#contextual_links']['group_menu'] = [ - 'route_parameters' => [ - 'group' => $this->getContext('group')->getContextData()->getValue()->id(), - 'group_content_menu' => $menu_instance->id(), - ], - ]; + if ($group = $this->getContext('group')->getContextData()->getValue()) { + if ($group instanceof Group) { + $group_id = $this->getContext('group')->getContextData()->getValue()->id(); + } + } + // No group entity found; check if the node page is linked to group through group_content + if (empty($group_id) && $group = \Drupal::request()->attributes->get('group')) { + if ($group instanceof Group) { + $group_id = $group->id(); + } + } + if (empty($group_id) && $node = \Drupal::request()->attributes->get('node')) { + if ($node instanceof NodeInterface) { - foreach (GroupContent::loadByEntity($node) as $group_content) { + foreach (GroupRelationship::loadByEntity($node) as $group_content) { + // This works best if a node is linked to one group; if its linked to more than one + // then it will grab the group entity->id of the last linked group. + $group_id = $group_content->gid->entity->id(); + + } + } + } + + if (!empty($group_id)) { + $build['#contextual_links']['group_menu'] = [ + 'route_parameters' => [ + 'group' => $group_id, + 'group_content_menu' => $menu_instance->id(), + ], + ]; + } } if ($menu_instance) { - $build['#theme'] = 'menu__group_menu__' . strtr($menu_instance->bundle(), '-', '_'); + $build['#theme'] = 'menu__group_menu'; } return $build; } @@ -245,6 +276,7 @@ class GroupMenuBlock extends BlockBase implements ContainerFactoryPluginInterfac 'route.menu_active_trails:group-menu-' . $this->getDerivativeId(), // We also vary by the active group as found by RouteGroupCacheContext. 'route.group', + 'route', ]; return Cache::mergeContexts(parent::getCacheContexts(), $tags); } @@ -261,6 +293,20 @@ class GroupMenuBlock extends BlockBase implements ContainerFactoryPluginInterfac */ public function getMenuInstance() { $entity = $this->getContext('group')->getContextData()->getValue(); + // No group entity found; check if the node page is linked to group through group_content + if (!$entity && ($group = \Drupal::request()->attributes->get('group'))) { + if ($group instanceof Group) { + $entity = $group; + } + } + + if (!$entity && ($node = \Drupal::request()->attributes->get('node'))) { + if ($node instanceof NodeInterface) { + if (!empty($this->getGroupIdsByEntity($node))) { + $entity = \Drupal\group\Entity\Group::load($this->getGroupIdsByEntity($node)[0]); + } + } + } // Don't load menu for group entities that are new/unsaved. if (!$entity || $entity->isNew()) { return NULL; @@ -281,6 +327,26 @@ class GroupMenuBlock extends BlockBase implements ContainerFactoryPluginInterfac return NULL; } + /** + * Returns a name for the menu. + * + * @param object $entity + * A node entity. + * + * @return string + * The gid. + */ + public function getGroupIdsByEntity($entity) { + $group_ids = []; + - $group_contents = GroupContent::loadByEntity($entity); + $group_contents = GroupRelationship::loadByEntity($entity); + foreach ($group_contents as $group_content) { + $group_ids[] = $group_content->getGroup()->id(); + } + + return $group_ids; + } + /** * Returns a name for the menu. * @@ -301,17 +367,4 @@ class GroupMenuBlock extends BlockBase implements ContainerFactoryPluginInterfac } return $this->menuName; } - - /** - * The menu link tree manipulators to apply. - * - * @see Drupal\Core\Menu\MenuLinkTreeInterface::transform() - */ - protected function getMenuManipulators(): array { - return [ - ['callable' => 'menu.default_tree_manipulators:checkAccess'], - ['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'], - ]; - } - }