You are here

function social_group_preprocess_group in Open Social 8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  2. 8.2 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  3. 8.3 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  4. 8.4 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  5. 8.5 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  6. 8.6 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  7. 8.7 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  8. 8.8 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  9. 10.3.x modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  10. 10.0.x modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  11. 10.1.x modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  12. 10.2.x modules/social_features/social_group/social_group.module \social_group_preprocess_group()

Prepares variables for profile templates.

Default template: profile.html.twig.

Parameters

array $variables: An associative array containing:

  • elements: An array of elements to display in view mode.
  • profile: The profile object.
  • view_mode: View mode; e.g., 'full', 'teaser', etc.

File

modules/social_features/social_group/social_group.module, line 218
The Social group module.

Code

function social_group_preprocess_group(array &$variables) {

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = $variables['group'];
  $variables['joined'] = FALSE;
  $variables['closed_group'] = FALSE;
  $variables['cta'] = '';
  $variables['closed_group_lock'] = FALSE;
  $account = \Drupal::currentUser();
  $group_type_id = $group
    ->getGroupType()
    ->id();
  if ($group_type_id == 'closed_group') {
    $variables['closed_group_lock'] = TRUE;
  }

  // Set joined to true for teaser when current logged in
  // user is member of the group.
  if ($group
    ->getMember($account)) {
    $variables['joined'] = TRUE;
    if ($group
      ->hasPermission('leave group', $account)) {

      // @todo switch this to get URL from routes correctly.
      $variables['group_operations_url'] = Url::fromUserInput('/group/' . $group
        ->id() . '/leave');
    }
  }
  elseif ($group
    ->hasPermission('join group', $account)) {

    // @todo switch this to get URL from routes correctly.
    $variables['group_operations_url'] = Url::fromUserInput('/group/' . $group
      ->id() . '/join');

    // If the group type is a closed_group.
    if ($group_type_id == 'closed_group' && !$group
      ->hasPermission('manage all groups', $account)) {

      // Users can only be invited.
      $variables['closed_group'] = TRUE;
      $variables['cta'] = t('Invitation only');
    }
  }

  // Add the hero styled image.
  if (!empty($group->field_group_image->entity)) {
    $variables['group_hero_styled_image_url'] = ImageStyle::load('social_xx_large')
      ->buildUrl($group->field_group_image->entity
      ->getFileUri());
  }

  // Add group edit url for management.
  if ($group instanceof Group) {

    // Get the current route name to check if
    // the user is on the edit or delete page.
    $route = \Drupal::routeMatch()
      ->getRouteName();
    if (!in_array($route, [
      'entity.group.edit_form',
      'entity.group.delete_form',
    ])) {
      if ($group
        ->access('update', $account)) {
        $variables['group_edit_url'] = $group
          ->toUrl('edit-form')
          ->toString();
        $variables['#cache']['contexts'][] = 'route.name';
      }
    }
  }

  // Count number of group members.
  $members = $group
    ->getMembers();
  $profile_storage = \Drupal::entityTypeManager()
    ->getStorage('profile');
  $members_count = 0;
  if ($members && $profile_storage) {
    foreach ($members as $member) {
      $member_account = $member
        ->getUser();
      if ($member_account instanceof User) {
        $member_profile = $profile_storage
          ->loadByUser($member_account, 'profile');
        if ($member_profile instanceof Profile) {
          $members_count++;
        }
      }
    }
  }
  $variables['group_members'] = $members_count;
}