You are here

function social_group_preprocess_group in Open Social 8.9

Same name and namespace in other branches
  1. 8 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 208
The Social group module.

Code

function social_group_preprocess_group(array &$variables) {

  /** @var \Drupal\social_group\GroupStatistics $group_statistics */
  $group_statistics = \Drupal::service('social_group.group_statistics');

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = $variables['group'];
  $variables['title'] = $group
    ->label();
  $variables['joined'] = FALSE;
  $variables['closed_group'] = FALSE;
  $variables['cta'] = '';
  $variables['closed_group_lock'] = $variables['secret_group_shield'] = FALSE;
  $group_type_id = $group
    ->getGroupType()
    ->id();
  if (!array_key_exists('direct', _social_group_get_join_methods($group))) {
    $variables['closed_group_lock'] = TRUE;
  }
  $variables['group_type_id'] = $group_type_id;
  $variables['group_type'] = $group
    ->getGroupType()
    ->label();
  $group_settings_help = [
    '#theme' => 'group_settings_help',
    '#group_type' => $group
      ->getGroupType()
      ->label(),
    '#join_method' => _social_group_get_join_methods($group),
    '#allowed_visibility' => _social_group_get_allowed_visibility($group),
  ];
  $variables['group_settings_help'] = \Drupal::service('renderer')
    ->renderPlain($group_settings_help);
  $account = \Drupal::currentUser();

  // 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)) {
      $variables['group_operations_url'] = Url::fromRoute('entity.group.leave', [
        'group' => $group
          ->id(),
      ]);
    }
  }
  elseif ($group
    ->hasPermission('join group', $account)) {

    // @todo switch this to get URL from routes correctly.
    $variables['group_operations_url'] = Url::fromRoute('entity.group.join', [
      'group' => $group
        ->id(),
    ]);
    if ($group_type_id === 'flexible_group') {
      $join_methods = $group
        ->get('field_group_allowed_join_method')
        ->getValue();
      $direct_option = in_array('direct', array_column($join_methods, 'value'), FALSE);
      if (!$direct_option) {
        $variables['group_operations_url'] = Url::fromRoute('entity.group.join', [
          'group' => $group
            ->id(),
        ]);
        $variables['closed_group'] = TRUE;
        $variables['cta'] = t('Invitation only');
      }
    }
  }
  elseif ($group_type_id == 'closed_group' && !$group
    ->hasPermission('manage all groups', $account)) {

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

  // Add the hero styled image.
  if ($group
    ->hasField('field_group_image') && !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';
      }
    }

    // Ensure all groups get the group.type differentiating labels in teasers
    // hero's and full nodes.
    // But also if the allowed join method is there, this should be added.
    $variables['#cache']['contexts'][] = 'group.type';
    if ($group
      ->hasField('field_group_allowed_join_method') && !empty($group
      ->getFieldValue('field_group_allowed_join_method', 'value'))) {
      $variables['#cache']['contexts'][] = 'social_group_join_method';
    }
  }

  // Count number of group members.
  $variables['group_members'] = $group_statistics
    ->getGroupMemberCount($group);

  // Prepare variables for statistic block.
  if ($variables['view_mode'] === 'statistic') {

    // Add context, since we render join / invite only etc links in the block.
    $variables['#cache']['contexts'][] = 'group';
    $variables['#cache']['contexts'][] = 'user';
    $about_url = Url::fromRoute('view.group_information.page_group_about', [
      'group' => $group
        ->id(),
    ]);
    $variables['about_url'] = $about_url;
    if ($group
      ->getGroupType()
      ->hasContentPlugin('group_node:event')) {
      $variables['group_events'] = $group_statistics
        ->getGroupNodeCount($group, 'event');
      $variables['group_events_label'] = \Drupal::translation()
        ->formatPlural($variables['group_events'], 'event', 'events');
    }
    if ($group
      ->getGroupType()
      ->hasContentPlugin('group_node:topic')) {
      $variables['group_topics'] = $group_statistics
        ->getGroupNodeCount($group, 'topic');
      $variables['group_topics_label'] = \Drupal::translation()
        ->formatPlural($variables['group_topics'], 'topic', 'topics');
    }
  }
}