Unable to create dynamic group operations

Created on 8 December 2023, 12 months ago
Updated 3 August 2024, 4 months ago

Problem/Motivation

In case there are no links showing up for a particular role (based on group permissions), you cannot create any new links dynamically using hook_group_operations_alter

Steps to reproduce

  1. Create a group type and a group of that type
  2. Login with a role with no permissions for the group (easiest might be as anonymous/logged out)
  3. Add the following hook (change the hook with module name and the role check depending on the previous point)
    function hook_group_operations_alter(array &$operations, GroupInterface $group) {
        // Make sure there is a button to direct anonymous users to login page
        if (\Drupal::currentUser()->isAnonymous()) {
          $operations['group-login-join'] = [
              'title' => 'Login To Join',
              'url' => new Url('user.login'),
              'weight' => -1
          ];
        }
    }
    
  4. Go to the group page
  5. You will not see the "Login To Join" button

Proposed resolution

/src/Plugin/Block/GroupOperationsBlock.php's build() function calls $this->moduleHandler->alter('group_operations', $links, $group); only if $links is not empty. This should be called outside the if condition. See the patch attached.

🐛 Bug report
Status

RTBC

Version

3.2

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024