You are here

function opigno_messaging_get_groups in Opigno messaging 8

Same name and namespace in other branches
  1. 3.x opigno_messaging.module \opigno_messaging_get_groups()

Helper function to get all groups that current user can message to.

1 call to opigno_messaging_get_groups()
opigno_messaging_get_all_users in ./opigno_messaging.module
Helper function to get all users that current user can message to.

File

./opigno_messaging.module, line 323
Contains opigno_messaging.module.

Code

function opigno_messaging_get_groups($group_type) {
  $current_user = \Drupal::currentUser();
  $groups = \Drupal::entityTypeManager()
    ->getStorage('group')
    ->loadByProperties([
    'type' => $group_type,
  ]);
  if (!$current_user
    ->hasPermission('message all groups')) {
    $groups = array_filter($groups, function ($group) use ($current_user) {

      /** @var \Drupal\group\Entity\GroupInterface $group */
      return $group
        ->getMember($current_user) !== FALSE;
    });
  }
  return array_map(function ($group) {

    /** @var \Drupal\group\Entity\Group $group */
    return [
      'entity_id' => $group
        ->id(),
      'title' => $group
        ->label(),
    ];
  }, $groups);
}