create a menu link to a view that takes group ID as contextual argument

Created on 2 November 2018, about 6 years ago
Updated 3 August 2024, 4 months ago

Hello,

I have created a view that list all the group content of a group passed in the path :
/group/%group/documents-internes.

I cannot set the menu item as a normal menu item directly in views because of the %group.
So I'd like to create a menu custom link that take the current contextual group Id and pass it to my view.
in my links.menu.yml :

dash.docs:
  title: 'docs'
  class: Drupal\dashbd\Plugin\Menu\GroupLink
  route_name: view.tableau_documents_par_territoire.page_3
  parent: tools
  weight: 100
  route_parameters: { group: '1' }

I've tried with a lot of things but I have not found yet.
I tried with a class that can change the route_parameters but I have not found to to find the group id :

namespace Drupal\dashbd\Plugin\Menu;

use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupContent;
use Drupal\group\Context\GroupRouteContextTrait;

/**
 * Represents a menu link for a user profile edition.
 */
class GroupLink extends MenuLinkDefault {

 use GroupRouteContextTrait;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
    $group = $this->getGroupFromRoute();
    kint($group->id);
  }


public function getRouteParameters() {
    // If the user is not Anonymous.
      // return the godd current grop ID.
      return ['group' => '1'];
  }
}

So is there a way to do properly just like the links in the operation group ?

Thanks,
Didier

💬 Support request
Status

Needs review

Component

Code

Created by

🇫🇷France did1979

Live updates comments and jobs are added and updated live.
  • views

    Involves, uses, or integrates with views. In Drupal 8 core, use the “VDC” tag instead.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • I'm working with the module now and seems to me that custom node is needed in order to make menu links that reference the .
    Seems pretty important to me.
    I don't know how I would go about it.
    How about a new menu called 'group' which provides links to ordinary group members to :
    - view members
    - view content (by type)
    - add content (by type)
    - manage membership
    The menu would be defined as a config item in config/install
    The menu links, each pointing to its own class would be defined in the group.links.menu.yml
    Would this not be a great addition to the module?

  • Update:
    I also found the group content menu but it seemed over complicated and i couldn't make it work. In any case I'm not looking for a menu with links to content but links to actions.
    So then I found the group operations block which doesn't work as expected but might be good enough.
    I also researched into how one might make a token that could be put into a menu link path like
    /group//members
    where the menu item would only show in a group context, and the would be replaced by the current group ID OR the link would redirect to the current group. However I couldn't work out how to do this.

Production build 0.71.5 2024