You are here

function social_group_request_preprocess_group in Open Social 10.3.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_request/social_group_request.module \social_group_request_preprocess_group()
  2. 10.0.x modules/social_features/social_group/modules/social_group_request/social_group_request.module \social_group_request_preprocess_group()
  3. 10.1.x modules/social_features/social_group/modules/social_group_request/social_group_request.module \social_group_request_preprocess_group()
  4. 10.2.x modules/social_features/social_group/modules/social_group_request/social_group_request.module \social_group_request_preprocess_group()

Implements hook_preprocess_group().

1 string reference to 'social_group_request_preprocess_group'
social_group_request_theme_registry_alter in modules/social_features/social_group/modules/social_group_request/social_group_request.module
Implements hook_theme_registry_alter().

File

modules/social_features/social_group/modules/social_group_request/social_group_request.module, line 132
Primary module hooks for social_group_request module.

Code

function social_group_request_preprocess_group(&$variables) {

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = $variables['group'];
  $group_type = $group
    ->getGroupType();
  $account = \Drupal::currentUser();

  // If the user is already a member we don't bother processing any further.
  if ($group
    ->getMember($account)) {
    return;
  }
  if (!$group_type
    ->hasContentPlugin('group_membership_request')) {
    return;
  }

  // If user has a pending invite we should skip the request button.
  if (\Drupal::hasService('ginvite.invitation_loader')) {

    /** @var \Drupal\ginvite\GroupInvitationLoader $loader */
    $loader = \Drupal::service('ginvite.invitation_loader');
    $group_invites = count($loader
      ->loadByProperties([
      'gid' => $group
        ->id(),
      'uid' => $account
        ->id(),
    ]));
    if (NULL !== $group_invites && $group_invites > 0) {
      return;
    }
  }
  $group_types = [
    'flexible_group',
  ];
  \Drupal::moduleHandler()
    ->alter('social_group_request', $group_types);
  if (in_array($group_type
    ->id(), $group_types)) {
    $join_methods = $group
      ->get('field_group_allowed_join_method')
      ->getValue();
    $request_option = in_array('request', array_column($join_methods, 'value'), FALSE);
    if (!$request_option) {
      $variables['allow_request'] = FALSE;
      return;
    }
  }
  else {
    $allow_request = $group
      ->get('allow_request');
    if ($allow_request
      ->isEmpty() || $allow_request->value == 0) {
      $variables['allow_request'] = FALSE;
      return;
    }
  }
  if ($account
    ->isAnonymous()) {
    $variables['anonymous_request'] = TRUE;
    $variables['group_operations_url'] = Url::fromRoute('social_group_request.anonymous_request_membership', [
      'group' => $group
        ->id(),
    ]);
    $variables['#attached']['library'][] = 'core/drupal.dialog.ajax';
    $variables['#attached']['library'][] = 'social_group_request/social_group_popup';
    return;
  }
  if (!$group
    ->hasPermission('request group membership', $account) || !$group
    ->hasField('allow_request')) {
    $variables['allow_request'] = FALSE;
    return;
  }
  $variables['closed_group'] = TRUE;
  $variables['allow_request'] = TRUE;
  $variables['group_operations_url'] = Url::fromRoute('grequest.request_membership', [
    'group' => $group
      ->id(),
  ]);
  $variables['cta'] = t('Request to join');
  $contentTypeConfigId = $group
    ->getGroupType()
    ->getContentPlugin('group_membership_request')
    ->getContentTypeConfigId();
  $request = \Drupal::entityQuery('group_content')
    ->condition('type', $contentTypeConfigId)
    ->condition('gid', $group
    ->id())
    ->condition('entity_id', $account
    ->id())
    ->condition('grequest_status', GroupMembershipRequest::REQUEST_PENDING)
    ->count()
    ->execute();
  if ($request > 0) {
    $variables['requested'] = TRUE;
    $variables['group_operations_url'] = Url::fromRoute('social_group_request.cancel_request', [
      'group' => $group
        ->id(),
    ]);
  }
  $variables['#attached']['library'][] = 'social_group_request/social_group_popup';
  $variables['#attached']['library'][] = 'social_group_request/social_group_request_popup';
  $variables['#attached']['library'][] = 'core/drupal.dialog.ajax';
  $variables['#cache']['tags'][] = 'request-membership:' . $group
    ->id();
}