Commit 340a0fa2 authored by webchick's avatar webchick

Issue #2225605 by znerol: Use request stack in form builder.

parent 427cd103
......@@ -62,6 +62,7 @@ function authorize_access_allowed() {
// Prepare a minimal bootstrap.
drupal_bootstrap(DRUPAL_BOOTSTRAP_PAGE_CACHE);
$request = \Drupal::request();
\Drupal::service('request_stack')->push($request);
// We have to enable the user and system modules, even to check access and
// display errors via the maintenance theme.
......
......@@ -122,9 +122,7 @@ services:
arguments: [default]
form_builder:
class: Drupal\Core\Form\FormBuilder
arguments: ['@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@url_generator', '@string_translation', '@?csrf_token', '@?http_kernel']
calls:
- [setRequest, ['@?request=']]
arguments: ['@module_handler', '@keyvalue.expirable', '@event_dispatcher', '@url_generator', '@string_translation', '@request_stack', '@?csrf_token', '@?http_kernel']
keyvalue:
class: Drupal\Core\KeyValueStore\KeyValueFactory
arguments: ['@service_container', '@settings']
......
......@@ -24,6 +24,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
......@@ -64,11 +65,11 @@ class FormBuilder implements FormBuilderInterface {
protected $urlGenerator;
/**
* The current request.
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\Request
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $request;
protected $requestStack;
/**
* The CSRF token generator to validate the form token.
......@@ -120,17 +121,20 @@ class FormBuilder implements FormBuilderInterface {
* The URL generator.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The translation manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack.
* @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
* The CSRF token generator.
* @param \Drupal\Core\HttpKernel $http_kernel
* The HTTP kernel.
*/
public function __construct(ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, TranslationInterface $string_translation, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) {
public function __construct(ModuleHandlerInterface $module_handler, KeyValueExpirableFactoryInterface $key_value_expirable_factory, EventDispatcherInterface $event_dispatcher, UrlGeneratorInterface $url_generator, TranslationInterface $string_translation, RequestStack $request_stack, CsrfTokenGenerator $csrf_token = NULL, HttpKernel $http_kernel = NULL) {
$this->moduleHandler = $module_handler;
$this->keyValueExpirableFactory = $key_value_expirable_factory;
$this->eventDispatcher = $event_dispatcher;
$this->urlGenerator = $url_generator;
$this->stringTranslation = $string_translation;
$this->requestStack = $request_stack;
$this->csrfToken = $csrf_token;
$this->httpKernel = $http_kernel;
}
......@@ -188,7 +192,8 @@ public function buildForm($form_id, array &$form_state) {
$form_id = $this->getFormId($form_id, $form_state);
if (!isset($form_state['input'])) {
$form_state['input'] = $form_state['method'] == 'get' ? $this->request->query->all() : $this->request->request->all();
$request = $this->requestStack->getCurrentRequest();
$form_state['input'] = $form_state['method'] == 'get' ? $request->query->all() : $request->request->all();
}
if (isset($_SESSION['batch_form_state'])) {
......@@ -811,8 +816,9 @@ public function validateForm($form_id, &$form, &$form_state) {
// matches the current user's session.
if (isset($form['#token'])) {
if (!$this->csrfToken->validate($form_state['values']['form_token'], $form['#token'])) {
$path = $this->request->attributes->get('_system_path');
$query = UrlHelper::filterQueryParameters($this->request->query->all());
$request = $this->requestStack->getCurrentRequest();
$path = $request->attributes->get('_system_path');
$query = UrlHelper::filterQueryParameters($request->query->all());
$url = $this->urlGenerator->generateFromPath($path, array('query' => $query));
// Setting this error will cause the form to fail validation.
......@@ -957,8 +963,9 @@ public function redirectForm($form_state) {
}
}
}
$url = $this->urlGenerator->generateFromPath($this->request->attributes->get('_system_path'), array(
'query' => $this->request->query->all(),
$request = $this->requestStack->getCurrentRequest();
$url = $this->urlGenerator->generateFromPath($request->attributes->get('_system_path'), array(
'query' => $request->query->all(),
'absolute' => TRUE,
));
return new RedirectResponse($url);
......@@ -1226,7 +1233,8 @@ public function setErrorByName($name, array &$form_state, $message = '') {
}
if ($record) {
$form_state['errors'][$name] = $message;
$this->request->attributes->set('_form_errors', TRUE);
$request = $this->requestStack->getCurrentRequest();
$request->attributes->set('_form_errors', TRUE);
if ($message) {
$this->drupalSetMessage($message, 'error');
}
......@@ -1241,7 +1249,8 @@ public function setErrorByName($name, array &$form_state, $message = '') {
*/
public function clearErrors(array &$form_state) {
$form_state['errors'] = array();
$this->request->attributes->set('_form_errors', FALSE);
$request = $this->requestStack->getCurrentRequest();
$request->attributes->set('_form_errors', FALSE);
}
/**
......@@ -1255,7 +1264,8 @@ public function getErrors(array $form_state) {
* {@inheritdoc}
*/
public function getAnyErrors() {
return (bool) $this->request->attributes->get('_form_errors');
$request = $this->requestStack->getCurrentRequest();
return (bool) $request->attributes->get('_form_errors');
}
/**
......@@ -1721,14 +1731,15 @@ protected function doFlattenOptions(array $array) {
* A response object.
*/
protected function sendResponse(Response $response) {
$event = new FilterResponseEvent($this->httpKernel, $this->request, HttpKernelInterface::MASTER_REQUEST, $response);
$request = $this->requestStack->getCurrentRequest();
$event = new FilterResponseEvent($this->httpKernel, $request, HttpKernelInterface::MASTER_REQUEST, $response);
$this->eventDispatcher->dispatch(KernelEvents::RESPONSE, $event);
// Prepare and send the response.
$event->getResponse()
->prepare($this->request)
->prepare($request)
->send();
$this->httpKernel->terminate($this->request, $response);
$this->httpKernel->terminate($request, $response);
}
/**
......@@ -1808,13 +1819,6 @@ protected function currentUser() {
return $this->currentUser;
}
/**
* {@inheritdoc}
*/
public function setRequest(Request $request) {
$this->request = $request;
}
/**
* Wraps batch_get().
*/
......
......@@ -635,12 +635,4 @@ public function setValue($element, $value, &$form_state);
*/
public function flattenOptions(array $array);
/**
* Sets the request object to use.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*/
public function setRequest(Request $request);
}
......@@ -36,10 +36,7 @@ public function userPage(Request $request) {
$response = $this->redirect('user.view', array('user' => $user->id()));
}
else {
// Sets the proper request.
// @todo Remove when the request object is synchronized.
$form_builder = $this->formBuilder();
$form_builder->setRequest($request);
$response = $form_builder->getForm('Drupal\user\Form\UserLoginForm');
}
return $response;
......
......@@ -389,6 +389,7 @@ function simpletest_script_bootstrap() {
$container = $kernel->getContainer();
$container->enterScope('request');
$container->set('request', $request, 'request');
$container->get('request_stack')->push($request);
$module_handler = $container->get('module_handler');
// @todo Remove System module. Only needed because \Drupal\Core\Datetime\Date
......
......@@ -12,6 +12,7 @@
use Drupal\Core\Session\AccountInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
/**
......@@ -142,8 +143,9 @@ protected function tearDown() {
* Sets up a new form builder object to test.
*/
protected function setupFormBuilder() {
$this->formBuilder = new TestFormBuilder($this->moduleHandler, $this->keyValueExpirableFactory, $this->eventDispatcher, $this->urlGenerator, $this->translationManager, $this->csrfToken, $this->httpKernel);
$this->formBuilder->setRequest($this->request);
$request_stack = new RequestStack();
$request_stack->push($this->request);
$this->formBuilder = new TestFormBuilder($this->moduleHandler, $this->keyValueExpirableFactory, $this->eventDispatcher, $this->urlGenerator, $this->translationManager, $request_stack, $this->csrfToken, $this->httpKernel);
$this->formBuilder->setCurrentUser($this->account);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment