diff --git a/core/core.services.yml b/core/core.services.yml index cc76c1dfd909884240983b213f06d50589a8fbe3..4e6b575dcc3c149af043474a756096133700f320 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1200,7 +1200,7 @@ services: arguments: ['@resolver_manager.entity'] ajax_response.subscriber: class: Drupal\Core\EventSubscriber\AjaxResponseSubscriber - arguments: ['@ajax_response.attachments_processor'] + autowire: true form_ajax_subscriber: class: Drupal\Core\Form\EventSubscriber\FormAjaxSubscriber arguments: ['@form_ajax_response_builder', '@string_translation', '@messenger'] diff --git a/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php index 9869f72882694c8844bccaabe221c82df963b417..8e37afbcdf9c51ac94132a3c4dcbf773fe9780f3 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AjaxResponseSubscriber.php @@ -4,7 +4,7 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Ajax\AjaxResponse; -use Drupal\Core\Render\AttachmentsResponseProcessorInterface; +use Symfony\Component\DependencyInjection\Attribute\AutowireServiceClosure; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\Event\RequestEvent; @@ -15,22 +15,16 @@ */ class AjaxResponseSubscriber implements EventSubscriberInterface { - /** - * The AJAX response attachments processor service. - * - * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface - */ - protected $ajaxResponseAttachmentsProcessor; - /** * Constructs an AjaxResponseSubscriber object. * - * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $ajax_response_attachments_processor - * The AJAX response attachments processor service. + * @param \Closure $processorClosure + * The AJAX response attachments processor service, wrapped in a closure. */ - public function __construct(AttachmentsResponseProcessorInterface $ajax_response_attachments_processor) { - $this->ajaxResponseAttachmentsProcessor = $ajax_response_attachments_processor; - } + public function __construct( + #[AutowireServiceClosure('ajax_response.attachments_processor')] + protected \Closure $processorClosure + ) {} /** * Request parameter to indicate that a request is a Drupal Ajax request. @@ -59,7 +53,7 @@ public function onRequest(RequestEvent $event) { public function onResponse(ResponseEvent $event) { $response = $event->getResponse(); if ($response instanceof AjaxResponse) { - $this->ajaxResponseAttachmentsProcessor->processAttachments($response); + ($this->processorClosure)()->processAttachments($response); // IE 9 does not support XHR 2 (http://caniuse.com/#feat=xhr2), so // for that browser, jquery.form submits requests containing a file upload diff --git a/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php b/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php index b4480f160bb68547d6d1cb8fe5d8c275b726cb7b..7dda4f4bc2650de98f50aefee20aff61235aecc1 100644 --- a/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Ajax/CommandsTest.php @@ -39,7 +39,7 @@ public function testAttachedSettings() { ]); $ajax_response_attachments_processor = \Drupal::service('ajax_response.attachments_processor'); - $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor); + $subscriber = new AjaxResponseSubscriber(fn() => $ajax_response_attachments_processor); $event = new ResponseEvent( \Drupal::service('http_kernel'), new Request(), diff --git a/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php b/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php index 0033fd3b41ef4b5631c6172f6f8316b71f2f17d2..ac17d076509101a1551f3f2d294bf4466cdb29f3 100644 --- a/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php +++ b/core/tests/Drupal/Tests/Core/Ajax/AjaxResponseTest.php @@ -86,7 +86,7 @@ public function testPrepareResponseForIeFormRequestsWithFileUpload() { $response->headers->set('Content-Type', 'application/json; charset=utf-8'); $ajax_response_attachments_processor = $this->createMock('\Drupal\Core\Render\AttachmentsResponseProcessorInterface'); - $subscriber = new AjaxResponseSubscriber($ajax_response_attachments_processor); + $subscriber = new AjaxResponseSubscriber(fn() => $ajax_response_attachments_processor); $event = new ResponseEvent( $this->createMock('\Symfony\Component\HttpKernel\HttpKernelInterface'), $request,