You are here

public function EventRequestEnrollmentNotification::build in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_event/src/Plugin/Block/EventRequestEnrollmentNotification.php \Drupal\social_event\Plugin\Block\EventRequestEnrollmentNotification::build()
  2. 10.3.x modules/social_features/social_event/src/Plugin/Block/EventRequestEnrollmentNotification.php \Drupal\social_event\Plugin\Block\EventRequestEnrollmentNotification::build()
  3. 10.0.x modules/social_features/social_event/src/Plugin/Block/EventRequestEnrollmentNotification.php \Drupal\social_event\Plugin\Block\EventRequestEnrollmentNotification::build()
  4. 10.1.x modules/social_features/social_event/src/Plugin/Block/EventRequestEnrollmentNotification.php \Drupal\social_event\Plugin\Block\EventRequestEnrollmentNotification::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

modules/social_features/social_event/src/Plugin/Block/EventRequestEnrollmentNotification.php, line 120

Class

EventRequestEnrollmentNotification
Provides a 'Event requests notification' block.

Namespace

Drupal\social_event\Plugin\Block

Code

public function build() : array {

  // No event? Don't bother anymore.
  if (!$this->event instanceof NodeInterface) {
    return [];
  }

  // Don't continue if we don't have the correct enroll method for this event.
  if ((int) $this->event
    ->getFieldValue('field_enroll_method', 'value') !== EventEnrollmentInterface::ENROLL_METHOD_REQUEST) {
    return [];
  }

  // At this point we try to get the amount of pending requests.
  try {
    $requests = $this->entityTypeManager
      ->getStorage('event_enrollment')
      ->getQuery()
      ->condition('field_event.target_id', $this->event
      ->id())
      ->condition('field_request_or_invite_status.value', EventEnrollmentInterface::REQUEST_PENDING)
      ->condition('field_enrollment_status.value', '0')
      ->count()
      ->execute();
    if (!$requests) {
      return [];
    }
    return [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t('There @link to enroll in this event.', [
        '@link' => Link::fromTextAndUrl($this->translation
          ->formatPlural($requests, 'is (1) new request', 'are (@count) new requests'), Url::fromRoute('view.event_manage_enrollment_requests.page_manage_enrollment_requests', [
          'node' => $this->event
            ->id(),
        ]))
          ->toString(),
      ]),
      '#attributes' => [
        'class' => [
          'alert',
          'alert-warning',
        ],
      ],
    ];
  } catch (InvalidPluginDefinitionException $e) {
    $this->loggerFactory
      ->get('social_event')
      ->error($e
      ->getMessage());
  } catch (PluginNotFoundException $e) {
    $this->loggerFactory
      ->get('social_event')
      ->error($e
      ->getMessage());
  }

  // Catch all.
  return [];
}