ContentFormControllerSubscriber.php 2.6 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\Core\EventSubscriber\ContentFormControllerSubscriber.
6 7
 */

8
namespace Drupal\Core\EventSubscriber;
9

10
use Drupal\Core\Controller\HtmlFormController;
11
use Drupal\Core\DependencyInjection\ClassResolverInterface;
12 13
use Drupal\Core\Controller\ControllerResolverInterface;
use Drupal\Core\Form\FormBuilderInterface;
14 15 16
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
17 18
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
19 20

/**
21
 * Subscriber for setting wrapping form logic.
22
 */
23
class ContentFormControllerSubscriber implements EventSubscriberInterface, ContainerAwareInterface {
24

25
  use ContainerAwareTrait;
26 27 28 29

  /**
   * The controller resolver.
   *
30
   * @var  \Drupal\Core\DependencyInjection\ClassResolverInterface.
31
   */
32
  protected $classResolver;
33 34 35 36 37 38 39

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;
40 41

  /**
42
   * Constructs a new ContentFormControllerSubscriber object.
43
   *
44 45 46 47
   * @param  \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver
   *   The class resolver.
   * @param \Drupal\Core\Controller\ControllerResolverInterface $controller_resolver
   *   The class resolver.
48 49
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
50
   */
51 52 53
  public function __construct(ClassResolverInterface $class_resolver, ControllerResolverInterface $controller_resolver, FormBuilderInterface $form_builder) {
    $this->classResolver = $class_resolver;
    $this->controllerResolver = $controller_resolver;
54
    $this->formBuilder = $form_builder;
55 56 57
  }

  /**
58 59 60 61
   * Sets the _controllere on a request based on the request format.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   The event to process.
62
   */
63 64 65 66
  public function onRequestDeriveFormWrapper(GetResponseEvent $event) {
    $request = $event->getRequest();

    if ($form = $request->attributes->get('_form')) {
67
      $wrapper = new HtmlFormController($this->classResolver, $this->controllerResolver, $this->container, $form, $this->formBuilder);
68
      $request->attributes->set('_content', array($wrapper, 'getContentResult'));
69 70 71
    }
  }

72 73 74 75 76 77 78 79 80 81 82
  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = array('onRequestDeriveFormWrapper', 29);

    return $events;
  }
83
}