You are here

public function BadgeController::addPage in User Badges 8

Addpage.

Return value

[] Return links for badge creation.

1 string reference to 'BadgeController::addPage'
user_badges.routing.yml in ./user_badges.routing.yml
user_badges.routing.yml

File

src/Controller/BadgeController.php, line 56
Contains \Drupal\user_badges\Controller\BadgeController.

Class

BadgeController
Class BadgeController.

Namespace

Drupal\user_badges\Controller

Code

public function addPage() {
  $build = [
    '#theme' => 'item_list',
  ];
  $content = [];
  $types = [];

  // Only use badge types the user has access to.
  foreach ($this
    ->entityTypeManager()
    ->getStorage('badge_type')
    ->loadMultiple() as $type) {
    $access = $this
      ->entityTypeManager()
      ->getAccessControlHandler('badge')
      ->createAccess($type
      ->id(), NULL, [], TRUE);
    if ($access
      ->isAllowed()) {
      $content[] = $this->linkGenerator
        ->generate($type
        ->label(), new Url('user_badges.badge_controller_add', [
        'badge_type' => $type
          ->id(),
      ]));
      $types[] = $type;
    }
  }

  // Bypass the /admin/structure/badge/add listing if only one content type is available.
  if (count($types) == 1) {
    $type = array_shift($types);
    return $this
      ->redirect('user_badges.badge_controller_add', [
      'badge_type' => $type
        ->id(),
    ]);
  }
  $build['#items'] = $content;
  return $build;
}