You are here

public function EventController::addPage in Booking and Availability Management Tools for Drupal 8

Displays add event links for available event types.

Redirects to admin/bat/events/event/add/[type] if only one event type is available.

Return value

array|\Symfony\Component\HttpFoundation\RedirectResponse A render array for a list of the event types that can be added; however, if there is only one event type defined for the site, the function will return a RedirectResponse to the event add page for that one event type.

1 string reference to 'EventController::addPage'
bat_event.routing.yml in modules/bat_event/bat_event.routing.yml
modules/bat_event/bat_event.routing.yml

File

modules/bat_event/src/Controller/EventController.php, line 59
Contains \Drupal\bat_event\Controller\EventController.

Class

EventController
Returns responses for Type routes.

Namespace

Drupal\bat_event\Controller

Code

public function addPage() {
  $build = [
    '#theme' => 'bat_event_add_list',
    '#cache' => [
      'tags' => $this
        ->entityTypeManager()
        ->getDefinition('bat_event_type')
        ->getListCacheTags(),
    ],
  ];
  $content = [];

  // Only use event types the user has access to.
  foreach ($this
    ->entityTypeManager()
    ->getStorage('bat_event_type')
    ->loadMultiple() as $type) {
    $access = $this
      ->entityTypeManager()
      ->getAccessControlHandler('bat_event')
      ->createAccess($type
      ->id(), NULL, [], TRUE);
    if ($access
      ->isAllowed()) {
      $content[$type
        ->id()] = $type;
    }
  }

  // Bypass the add listing if only one event type is available.
  if (count($content) == 1) {
    $type = array_shift($content);
    return $this
      ->redirect('entity.bat_event.add_form', [
      'event_type' => $type
        ->id(),
    ]);
  }
  $build['#content'] = $content;
  return $build;
}