diff --git a/core/core.services.yml b/core/core.services.yml index fac198c..56f1467 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -566,8 +566,12 @@ services: class: Symfony\Component\HttpKernel\HttpKernel arguments: ['@event_dispatcher', '@controller_resolver', '@request_stack'] http_negotiation.format_negotiator: - class: Drupal\Core\FormatMapping + class: Drupal\Core\ContentNegotiation private: true + http_negotiation.wrapper_format_subscriber: + class: Drupal\Core\EventSubscriber\WrapperFormatSubscriber + tags: + - { name: event_subscriber } http_middleware.negotiation: class: Drupal\Core\StackMiddleware\NegotiationMiddleware arguments: ['@http_negotiation.format_negotiator'] diff --git a/core/lib/Drupal/Core/FormatMapping.php b/core/lib/Drupal/Core/ContentNegotiation.php similarity index 89% rename from core/lib/Drupal/Core/FormatMapping.php rename to core/lib/Drupal/Core/ContentNegotiation.php index 40e6aea..3322af8 100644 --- a/core/lib/Drupal/Core/FormatMapping.php +++ b/core/lib/Drupal/Core/ContentNegotiation.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\FormatMapping. + * Definition of Drupal\Core\ContentNegotiation. */ namespace Drupal\Core; @@ -15,7 +15,7 @@ * @todo Replace this class with a real content negotiation library based on * mod_negotiation. Development of that is a work in progress. */ -class FormatMapping { +class ContentNegotiation { /** * Gets the normalized type of a request. @@ -36,15 +36,12 @@ public function getContentType(Request $request) { return 'iframeupload'; } - if ($type = $request->query->get('dialogType')) { - return 'drupal_' . $type; - } - // Check all formats, if priority format is found return it. $first_found_format = FALSE; + $priority = array('html'); foreach ($request->getAcceptableContentTypes() as $mime_type) { $format = $request->getFormat($mime_type); - if ($format === 'html') { + if (in_array($format, $priority, TRUE)) { return $format; } if (!is_null($format) && !$first_found_format) { diff --git a/core/lib/Drupal/Core/EventSubscriber/WrapperFormatSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/WrapperFormatSubscriber.php new file mode 100644 index 0000000..f293c57 --- /dev/null +++ b/core/lib/Drupal/Core/EventSubscriber/WrapperFormatSubscriber.php @@ -0,0 +1,40 @@ +getRequest(); + if ($request->query->has(static::WRAPPER_FORMAT)) { + $request->setRequestFormat($request->query->get(static::WRAPPER_FORMAT)); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[KernelEvents::REQUEST][] = ['onRequest', 48]; + + return $events; + } + +} diff --git a/core/lib/Drupal/Core/Render/Element/RenderElement.php b/core/lib/Drupal/Core/Render/Element/RenderElement.php index 87b9f02..7600f62 100644 --- a/core/lib/Drupal/Core/Render/Element/RenderElement.php +++ b/core/lib/Drupal/Core/Render/Element/RenderElement.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Render\Element; +use Drupal\Core\EventSubscriber\WrapperFormatSubscriber; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginBase; use Drupal\Core\Render\Element; @@ -240,7 +241,7 @@ public static function preRenderAjaxForm($element) { $settings += array( 'url' => isset($settings['callback']) ? Url::fromRoute('system.ajax') : NULL, 'options' => array(), - 'dialogType' => 'ajax', + WrapperFormatSubscriber::WRAPPER_FORMAT => 'ajax', ); // @todo Legacy support. Remove in Drupal 8. diff --git a/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php b/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php index 8fa0173..295857d 100644 --- a/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php +++ b/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php @@ -7,7 +7,7 @@ namespace Drupal\Core\StackMiddleware; -use Drupal\Core\FormatMapping; +use Drupal\Core\ContentNegotiation; use Drupal\Core\ContentNegotiationInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -29,7 +29,7 @@ class NegotiationMiddleware implements HttpKernelInterface { /** * The content negotiator. * - * @var \Drupal\Core\FormatMapping + * @var \Drupal\Core\ContentNegotiation */ protected $negotiator; @@ -45,10 +45,10 @@ class NegotiationMiddleware implements HttpKernelInterface { * * @param \Symfony\Component\HttpKernel\HttpKernelInterface $app * The wrapper HTTP kernel - * @param \Drupal\Core\FormatMapping $negotiator + * @param \Drupal\Core\ContentNegotiation $negotiator * The content negotiator. */ - public function __construct(HttpKernelInterface $app, FormatMapping $negotiator) { + public function __construct(HttpKernelInterface $app, ContentNegotiation $negotiator) { $this->app = $app; $this->negotiator = $negotiator; } diff --git a/core/misc/ajax.js b/core/misc/ajax.js index 212a79f..b2eb6fb 100644 --- a/core/misc/ajax.js +++ b/core/misc/ajax.js @@ -272,7 +272,7 @@ else { ajax.options.url += '&'; } - ajax.options.url += 'dialogType=' + element_settings.dialogType; + ajax.options.url += '_wrapper_format=' + element_settings.dialogType; } // Bind the ajaxSubmit function to the element event. diff --git a/core/modules/ckeditor/js/ckeditor.js b/core/modules/ckeditor/js/ckeditor.js index 5d9e969..7f9dbf0 100644 --- a/core/modules/ckeditor/js/ckeditor.js +++ b/core/modules/ckeditor/js/ckeditor.js @@ -161,7 +161,7 @@ var $content = $('
'); $content.appendTo($target); new Drupal.ajax('ckeditor-dialog', $content.find('a').get(0), { - dialogType: 'modal', + _wrapper_format: 'modal', dialog: dialogSettings, selector: '.ckeditor-dialog-loading-link', url: url, diff --git a/core/modules/system/src/Tests/Ajax/DialogTest.php b/core/modules/system/src/Tests/Ajax/DialogTest.php index 28a3d0c..741ee5d 100644 --- a/core/modules/system/src/Tests/Ajax/DialogTest.php +++ b/core/modules/system/src/Tests/Ajax/DialogTest.php @@ -7,6 +7,7 @@ namespace Drupal\system\Tests\Ajax; +use Drupal\Core\EventSubscriber\WrapperFormatSubscriber; use Drupal\Core\Url; /** @@ -165,7 +166,7 @@ public function testDialog() { 'callback' => '::preview', 'event' => 'click', 'url' => Url::fromRoute('system.ajax')->toString(), - 'dialogType' => 'ajax', + WrapperFormatSubscriber::WRAPPER_FORMAT => 'ajax', 'submit' => [ '_triggering_element_name' => 'op', '_triggering_element_value' => 'Preview', diff --git a/core/tests/Drupal/Tests/Core/FormatMappingTest.php b/core/tests/Drupal/Tests/Core/ContentNegotationTest.php similarity index 65% rename from core/tests/Drupal/Tests/Core/FormatMappingTest.php rename to core/tests/Drupal/Tests/Core/ContentNegotationTest.php index f24e09b..d5f3081 100644 --- a/core/tests/Drupal/Tests/Core/FormatMappingTest.php +++ b/core/tests/Drupal/Tests/Core/ContentNegotationTest.php @@ -7,20 +7,20 @@ namespace Drupal\Tests\Core; -use Drupal\Core\FormatMapping; +use Drupal\Core\ContentNegotiation; use Drupal\Tests\UnitTestCase; use Symfony\Component\HttpFoundation\Request; /** - * @coversDefaultClass \Drupal\Core\FormatMapping - * @group FormatMapping + * @coversDefaultClass \Drupal\Core\ContentNegotiation + * @group ContentNegotiation */ -class FormatMappingTest extends UnitTestCase { +class ContentNegotiationTest extends UnitTestCase { /** - * @var \Drupal\Core\FormatMapping + * @var \Drupal\Core\ContentNegotiation */ - protected $formatMapping; + protected $contentNegotiation; /** * {@inheritdoc} @@ -28,7 +28,7 @@ class FormatMappingTest extends UnitTestCase { protected function setUp() { parent::setUp(); - $this->formatMapping = new FormatMapping; + $this->contentNegotiation = new ContentNegotiation; } /** @@ -40,7 +40,7 @@ public function testAjaxIframeUpload() { $request = new Request(); $request->attributes->set('ajax_iframe_upload', '1'); - $this->assertSame('iframeupload', $this->formatMapping->getContentType($request)); + $this->assertSame('iframeupload', $this->contentNegotiation->getContentType($request)); } /** @@ -51,21 +51,16 @@ public function testAjaxIframeUpload() { */ public function testAPriorityFormatIsFound($priority, $format) { $request = new Request(); + $request->setFormat($format['format'], $format['mime_type']); + $request->headers->set('Accept', sprintf('%s,application/json', $format['mime_type'])); - if ($format) { - $request->query->set('dialogType', $format); - } - - $this->assertSame($priority, $this->formatMapping->getContentType($request)); + $this->assertSame($priority, $this->contentNegotiation->getContentType($request)); } public function priorityFormatProvider() { return [ - ['drupal_dialog', 'dialog'], - ['drupal_modal', 'modal'], - ['drupal_ajax', 'ajax'], - ['html', ''], + ['html', ['format' => 'html', 'mime_type' => 'text/html']], ]; } @@ -78,7 +73,7 @@ public function testNoPriorityFormatIsFoundButReturnsTheFirstValidOne() { $request = new Request(); $request->headers->set('Accept', 'application/rdf+xml'); - $this->assertSame('rdf', $this->formatMapping->getContentType($request)); + $this->assertSame('rdf', $this->contentNegotiation->getContentType($request)); } /** @@ -89,7 +84,7 @@ public function testNoPriorityFormatIsFoundButReturnsTheFirstValidOne() { public function testUnknowContentTypeReturnsHtmlByDefault() { $request = new Request(); - $this->assertSame('html', $this->formatMapping->getContentType($request)); + $this->assertSame('html', $this->contentNegotiation->getContentType($request)); } /** @@ -101,7 +96,7 @@ public function testUnknowContentTypeButAjaxRequest() { $request = new Request(); $request->headers->set('X-Requested-With', 'XMLHttpRequest'); - $this->assertSame('ajax', $this->formatMapping->getContentType($request)); + $this->assertSame('ajax', $this->contentNegotiation->getContentType($request)); } }