You are here

function social_group_preprocess_group in Open Social 8.5

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  2. 8 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  3. 8.2 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  4. 8.3 modules/social_features/social_group/social_group.module \social_group_preprocess_group()
  5. 8.4 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 201
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');
  }
  elseif ($group_type_id == 'closed_group' && !$group
    ->hasPermission('manage all groups', $account)) {

    // Users can only be invited.
    $variables['group_operations_url'] = Url::fromUserInput('/group/' . $group
      ->id() . '/join');
    $variables['closed_group'] = TRUE;
    $variables['cta'] = t('Invitation only');
  }

  // Add the hero styled image.
  if (!empty($group
    ->get('field_group_image')->entity)) {

    // Fetch image style from field info.
    $original_image_style = $variables['content']['field_group_image'][0]['#image_style'] ?? '';
    $image_style = ImageStyle::load($original_image_style);
    if ($image_style instanceof ImageStyle) {
      $variables['group_hero_styled_image_url'] = $image_style
        ->buildUrl($group
        ->get('field_group_image')->entity
        ->getFileUri());

      // Check if this style is considered small.
      $overridden_image_style = Drupal::getContainer()
        ->get('social_group.hero_image')
        ->getGroupHeroImageStyle();
      if ($overridden_image_style !== $original_image_style) {
        $variables['group_hero_styled_image_url'] = ImageStyle::load($overridden_image_style)
          ->buildUrl($group
          ->get('field_group_image')->entity
          ->getFileUri());
      }
    }
  }

  // This should be determined regardless if there's an image or not.
  if (Drupal::getContainer()
    ->get('social_group.hero_image')
    ->isSmall()) {
    $variables['group_hero_small'] = TRUE;
  }

  // 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;
}