You are here

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

Displays add content links for available unit type bundles.

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

Return value

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

1 string reference to 'TypeController::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/TypeController.php, line 35
Contains \Drupal\bat_unit\Controller\TypeController.

Class

TypeController
Returns responses for Type routes.

Namespace

Drupal\bat_unit\Controller

Code

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

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

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