0

In my project every group member will only ever belong to a group; some group members have the Group Administrator role.

I would like to create a view block to satisfy this story: As a user with the Group Administrator role I need to see a list of all members in my group so that I can easily access their information from any page.
Putting it in another way: I need to show a block on every page that lists all members of the group that the currently logged in user is an administrator of.

The permission stuff is simple enough. What I'm struggling with is the contextual filter on the view, and I'm thinking now that I'll need to do some code customization. I can't just add "UID of currently logged in user". Am I missing the right configuration?

I just found this approach of making a custom block. I'll start going down that path but still very curious if I'm missing something.

Based on that link from Drupal.org, I'm currently doing the following:

/**
 * {@inheritdoc}
 */
public function access(AccountInterface $account, $return_as_object = FALSE) {
  return $this->groupMembership[0]->hasPermission('view group_membership content') &&
  count($this->groupMembership[0]->getGroup()->getMembers()) > 1;
}

/**
 * {@inheritdoc}
 */
public function build() {
  $build = [];

  //$build['group_member_block']['#markup'] = 'Implement GroupMemberBlock.';
  $build['group_member_block']['item_list'] = [
    '#theme' => 'item_list',
    '#list_type' => 'ul',
    '#wrapper_attributes' => [
      'class' => [
        'group-member-list'
      ]
    ],
    '#items' => $this->getGroupMembersItemList(),
  ];

  return $build;
}

protected function getGroupMembersItemList() {
  $group_members = $this->groupMembership[0]->getGroup()->getMembers();
  $items = [];
  foreach ($group_members as $member) {
    $user = $member->getUser();
    $items[] = [
      '#markup' => $this->getGroupMemberName($user),
    ];
  }

  return $items;
}

It is a pretty darn simple way to do it. For some reason, I expected to be able to hop right into views and build it though. Again, maybe I'm missing something simple.

  • As per your setup of "every group member will only ever belong to 1 group": what kind of results do you get if you create a view like "show me all group memberships"? I mean: is the list of results returned of such view equal to (only) all group memberships that the current user belongs to? – Pierre.Vriens May 26 '17 at 17:40
  • From what I can tell/have gathered so far, there isn't a way to provide a contextual filter that will "show me all group memberships for the group that the current user belongs to". Group seems to be working like taxonomy, in that if I'm on the group/[group_id] page I can get all members. – Chris Rockwell May 26 '17 at 18:08
0

One custom solution that is doing the exact same thing in my case is to create a view-block and show group content of type group membership. Then add the relationship Group content User, require this relationship, filter by plugin Group membership. Then add a contextual filter Group content: parent group. Add all fields you want to show in the view and use the relationship Group content user to display name, id, email etc.

Now the "custom" part in my solution is a tiny module that implements the hook_views_pre_view() and adds the gid of the currently logged in user to the $args array of that specific view like this: $args[0] = $gid;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.