You are here

function social_event_preprocess_page in Open Social 10.0.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/social_event.module \social_event_preprocess_page()
  2. 10.3.x modules/social_features/social_event/social_event.module \social_event_preprocess_page()
  3. 10.1.x modules/social_features/social_event/social_event.module \social_event_preprocess_page()
  4. 10.2.x modules/social_features/social_event/social_event.module \social_event_preprocess_page()

Implements hook_preprocess_page().

File

modules/social_features/social_event/social_event.module, line 963
The Social event module.

Code

function social_event_preprocess_page(array &$variables) {

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */
  $admin_context = \Drupal::service('router.admin_context');

  // Don't add the modal for admin pages.
  if ($admin_context
    ->isAdminRoute()) {
    return;
  }

  // Make sure we do the expensive checks only after we see that there's
  // something in the URL for nodes.
  if (\Drupal::routeMatch()
    ->getRouteName() === 'entity.node.canonical' && \Drupal::request()->query
    ->get('requested-enrollment')) {

    // Get current event and user id.
    $node = social_event_get_current_event();
    $uid = \Drupal::currentUser()
      ->id();

    // Only show the modal if the request method is set on this node
    // and if the 'requested-enrollment' parameter is TRUE.
    if ($node instanceof Node && (int) $node
      ->get('field_enroll_method')->value === EventEnrollmentInterface::ENROLL_METHOD_REQUEST) {

      // If there is a max of enrollments to this event and it's reached we
      // don't want users to be able to use the modal dialog to request to
      // enroll.
      if (\Drupal::hasService('social_event_max_enroll.service') && \Drupal::service('social_event_max_enroll.service')
        ->isEnabled($node) && !\Drupal::service('social_event_max_enroll.service')
        ->getEnrollmentsLeft($node)) {
        return;
      }

      // Query to see if the user is already enrolled,
      // has already requested or is already invited.
      $query = \Drupal::entityTypeManager()
        ->getStorage('event_enrollment')
        ->getQuery();
      $query
        ->condition('field_event.target_id', $node
        ->id())
        ->condition('field_account.target_id', $uid)
        ->condition('field_request_or_invite_status.value', [
        EventEnrollmentInterface::REQUEST_APPROVED,
        EventEnrollmentInterface::INVITE_INVITED,
        EventEnrollmentInterface::INVITE_PENDING_REPLY,
        EventEnrollmentInterface::INVITE_ACCEPTED_AND_JOINED,
        EventEnrollmentInterface::INVITE_INVALID_OR_EXPIRED,
      ], 'IN');
      $queryResult = (bool) $query
        ->execute();
      if ($queryResult === FALSE) {
        $url = Url::fromRoute('social_event.request_enroll_dialog', [
          'node' => $node
            ->id(),
        ]);
        $link = Link::fromTextAndUrl('', $url)
          ->toRenderable();
        $link['#attributes'] = [
          'id' => 'modal-trigger',
          'class' => [
            'use-ajax',
            'hidden',
          ],
        ];

        // Attach link and libraries.
        $variables['page']['content']['eventEnrollmentRequest'] = $link;
        $variables['page']['content']['eventEnrollmentRequest']['#attached']['library'] = [
          'core/drupal.dialog.ajax',
          'social_event/modal',
        ];
      }
    }
  }
}