BadgeController.php in User Badges 8
File
src/Controller/BadgeController.php
View source
<?php
namespace Drupal\user_badges\Controller;
use Drupal\acquia_connector\Client;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\user_badges\BadgeInterface;
use Drupal\user_badges\BadgeTypeInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BadgeController extends ControllerBase {
protected $linkGenerator;
public function __construct(LinkGeneratorInterface $linkGenerator) {
$this->linkGenerator = $linkGenerator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('link_generator'));
}
public function addPage() {
$build = [
'#theme' => 'item_list',
];
$content = [];
$types = [];
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;
}
}
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;
}
public function add(BadgeTypeInterface $badge_type) {
$badge = $this
->entityTypeManager()
->getStorage('badge')
->create([
'type' => $badge_type
->id(),
]);
return $this
->entityFormBuilder()
->getForm($badge);
}
public function addBadgeTitle(BadgeTypeInterface $badge_type) {
return $this
->t('Create @name', [
'@name' => $badge_type
->label(),
]);
}
}