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.