summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/EventSubscriber/DrunkControllerSubscriber.php
blob: bbd5be05cd023df6ad9769c0f977abaabdc8cb22 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php

/**
 * @file
 * Definition of Drupal\Core\EventSubscriber\DrunkControllerSubscriber.
 */

namespace Drupal\Core\EventSubscriber;

use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

use Drupal\Core\Config\Entity\Display;

/**
 * Controller injector for text/html responses.
 */
class DrunkControllerSubscriber implements EventSubscriberInterface {

  /**
   * Attaches a controller for blocks-driven HTML rendering, if appropriate.
   *
   * This determines if the Request and Route are appropriate candidates for
   * passing through block-driven rendering, and if so, attaches a controller
   * to do so.
   *
   * This is more or less the default case; we only don't take over if Drupal
   * is doing things like form processing, returning JSON or other things that
   * (generally) do not involve composing HTML.
   *
   */
  public function onDrunkenKernelController(FilterControllerEvent $event) {
    $request = $event->getRequest();
    // @todo implement a separate flag and remove this hardmapping
    if ('\Drupal\Core\Booze\DrunkController::respond' !== $request->attributes->get('_controller')) {
      return;
    }

    $route = $request->attributes->get('_route');

    if (!empty($route)) {
      $display = entity_load('booze_display', $route);
    }

    if (empty($display)) {
      // @todo put a real, meaningful exception here. and/or figure out fallback behavior.
      // throw new \Exception('no display');
      // $display = new Display(array('id' => 'new'));
      return;
    }

    $display->setMainContent($request->attributes->get('_content'));

    $request->attributes->set('_display', $display);
  }

  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::CONTROLLER][] = array('onDrunkenKernelController', 100);

    return $events;
  }
}