EventController.php in Booking and Availability Management Tools for Drupal 8
File
modules/bat_event/src/Controller/EventController.php
View source
<?php
namespace Drupal\bat_event\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\bat_event\EventInterface;
use Drupal\bat_event\EventTypeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class EventController extends ControllerBase implements ContainerInjectionInterface {
protected $request;
public function __construct(Request $request) {
$this->request = $request;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack')
->getCurrentRequest());
}
public function addPage() {
$build = [
'#theme' => 'bat_event_add_list',
'#cache' => [
'tags' => $this
->entityTypeManager()
->getDefinition('bat_event_type')
->getListCacheTags(),
],
];
$content = [];
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;
}
}
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;
}
public function add(EventTypeInterface $event_type) {
$type = $this
->entityTypeManager()
->getStorage('bat_event')
->create([
'type' => $event_type
->id(),
]);
$form = $this
->entityFormBuilder()
->getForm($type);
return $form;
}
public function addPageTitle(EventTypeInterface $event_type) {
return $this
->t('Create @name', [
'@name' => $event_type
->label(),
]);
}
public function editEvent(EventInterface $event) {
$input = $this->request->request
->all();
$programmed = isset($input['form_id']);
$input['form_id'] = 'bat_event_' . $event
->bundle() . '_edit_form';
$form = $this
->entityFormBuilder()
->getForm($event, 'default', [
'programmed' => $programmed,
'input' => $input,
]);
return $form;
}
}