If DrunkControllerSubscriber can't find a display, bail out.
[sandbox/eclipsegc/1441840.git] / core / lib / Drupal / Core / EventSubscriber / DrunkControllerSubscriber.php
1 <?php
2
3 /**
4 * @file
5 * Definition of Drupal\Core\EventSubscriber\DrunkControllerSubscriber.
6 */
7
8 namespace Drupal\Core\EventSubscriber;
9
10 use Symfony\Component\HttpKernel\KernelEvents;
11 use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
13
14 use Drupal\Core\Config\Entity\Display;
15
16 /**
17 * Controller injector for text/html responses.
18 */
19 class DrunkControllerSubscriber implements EventSubscriberInterface {
20
21 /**
22 * Attaches a controller for blocks-driven HTML rendering, if appropriate.
23 *
24 * This determines if the Request and Route are appropriate candidates for
25 * passing through block-driven rendering, and if so, attaches a controller
26 * to do so.
27 *
28 * This is more or less the default case; we only don't take over if Drupal
29 * is doing things like form processing, returning JSON or other things that
30 * (generally) do not involve composing HTML.
31 *
32 */
33 public function onDrunkenKernelController(FilterControllerEvent $event) {
34 $request = $event->getRequest();
35 // @todo implement a separate flag and remove this hardmapping
36 if ('\Drupal\Core\Booze\DrunkController::respond' !== $request->attributes->get('_controller')) {
37 return;
38 }
39
40 $route = $request->attributes->get('_route');
41
42 if (!empty($route)) {
43 $display = entity_load('booze_display', $route);
44 }
45
46 if (empty($display)) {
47 // @todo put a real, meaningful exception here. and/or figure out fallback behavior.
48 // throw new \Exception('no display');
49 // $display = new Display(array('id' => 'new'));
50 return;
51 }
52
53 $display->setMainContent($request->attributes->get('_content'));
54
55 $request->attributes->set('_display', $display);
56 }
57
58 /**
59 * Registers the methods in this class that should be listeners.
60 *
61 * @return array
62 * An array of event listener definitions.
63 */
64 public static function getSubscribedEvents() {
65 $events[KernelEvents::CONTROLLER][] = array('onDrunkenKernelController', 100);
66
67 return $events;
68 }
69 }