You are here

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

Displays add content links for available unit bundles.

Redirects to admin/bat/config/unit/add/[type] if only one unit bundle is available.

Return value

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

1 string reference to 'UnitController::addPage'
bat_unit.routing.yml in modules/bat_unit/bat_unit.routing.yml
modules/bat_unit/bat_unit.routing.yml

File

modules/bat_unit/src/Controller/UnitController.php, line 36
Contains \Drupal\bat_unit\Controller\UnitController.

Class

UnitController
Returns responses for Type routes.

Namespace

Drupal\bat_unit\Controller

Code

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

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

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