diff --git a/core/includes/common.inc b/core/includes/common.inc index a1885ae..1fcd1fd 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -24,6 +24,7 @@ use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Render\SafeString; use Drupal\Core\Render\Renderer; +use Drupal\Core\Render\HtmlResponseAttachmentsProcessor; use Drupal\Core\Site\Settings; use Drupal\Core\Url; use Symfony\Component\HttpFoundation\Response; @@ -529,16 +530,23 @@ function drupal_merge_attached(array $a, array $b) { * * @throws LogicException * When attaching something of a non-existing attachment type. - * - * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0. Specify - * attached resources and directives in the render array. */ function drupal_process_attached(array $elements) { - // Asset attachments are handled by \Drupal\Core\Asset\AssetResolver. - // Unsetting these parts preserved as a side-effect even though this function - // is deprecated. - foreach (array('library', 'drupalSettings') as $type) { - unset($elements['#attached'][$type]); + // If there is something to set, we should set it. + if (!empty($elements['#attached'])) { + // We merge the attachments with a static on + // HtmlResponseAttachmentsProcessor. If that property is empty, we have to + // set it first. + if (empty(HtmlResponseAttachmentsProcessor::$legacyAttached)) { + HtmlResponseAttachmentsProcessor::$legacyAttached = $elements['#attached']; + } + else { + HtmlResponseAttachmentsProcessor::$legacyAttached = + BubbleableMetadata::mergeAttachments( + HtmlResponseAttachmentsProcessor::$legacyAttached, + $elements['#attached'] + ); + } } } diff --git a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php index 759409a..c50e320 100644 --- a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php +++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php @@ -35,6 +35,16 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorInterface { /** + * An ['#attached'] array. + * + * Provided so that we can support drupal_processed_attached() as an API. + * + * @var array + * @todo Remove this in Drupal 9. + */ + public static $legacyAttached; + + /** * The asset resolver service. * * @var \Drupal\Core\Asset\AssetResolverInterface @@ -126,6 +136,10 @@ public function processAttachments(AttachmentsInterface $response) { // need to include. $response = $this->renderPlaceholders($response); + // Add the static attachment array to preserve drupal_process_attached(). + if (!empty(static::$legacyAttached)) { + $response->addAttachments(static::$legacyAttached); + } // Get a reference to the attachments. $attached = $response->getAttachments(); diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 355fd02..47eac67 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -469,8 +469,8 @@ function template_preprocess_book_export_html(&$variables) { $variables['language'] = $language_interface; $variables['language_rtl'] = ($language_interface->getDirection() == LanguageInterface::DIRECTION_RTL); // @todo: Commenting out the following line is wrong. - // @see https://www.drupal.org/node/2477223 -// $variables['head'] = drupal_get_html_head(); + // @see https://www.drupal.org/node/1338858 + // $variables['head'] = drupal_get_html_head(); // HTML element attributes. $attributes = array(); diff --git a/core/modules/system/src/Tests/Render/HtmlResponseAttachmentsTest.php b/core/modules/system/src/Tests/Render/HtmlResponseAttachmentsTest.php index 1441541..eb98cb8 100644 --- a/core/modules/system/src/Tests/Render/HtmlResponseAttachmentsTest.php +++ b/core/modules/system/src/Tests/Render/HtmlResponseAttachmentsTest.php @@ -41,6 +41,21 @@ public function testAttachments() { $this->assertEqual(1, count($this->xpath('//head/link[@type="application/rss+xml"]')), 'Link has proper content type.'); $this->assertEqual(1, count($this->xpath('//head/link[@href="test://url"]')), 'Link has URL.'); $this->assertEqual(1, count($this->xpath('//head/link[@title=""]')), 'Link has title.'); + + // Now repeat the process using drupal_process_attached(). + $this->drupalGet('/render_attached_test/teapot_dpa'); + $this->assertResponse(418); + + $this->drupalGet('/render_attached_test/head_dpa'); + $this->assertHeader('X-Test-Teapot', 'Teapot Mode Active'); + $this->assertHeader('X-Test-Teapot-Replace', 'Teapot replaced'); + $this->assertHeader('X-Test-Teapot-No-Replace', 'This value is not replaced,This one is added'); + + $this->drupalGet('/render_attached_test/feed_dpa'); + $this->assertEqual(1, count($this->xpath('//head/link[@rel="alternate"]')), 'Link has rel="alternate"'); + $this->assertEqual(1, count($this->xpath('//head/link[@type="application/rss+xml"]')), 'Link has proper content type.'); + $this->assertEqual(1, count($this->xpath('//head/link[@href="test://url"]')), 'Link has URL.'); + $this->assertEqual(1, count($this->xpath('//head/link[@title=""]')), 'Link has title.'); } } diff --git a/core/modules/system/tests/modules/render_attached_test/render_attached_test.routing.yml b/core/modules/system/tests/modules/render_attached_test/render_attached_test.routing.yml index e7329ef..40f8229 100644 --- a/core/modules/system/tests/modules/render_attached_test/render_attached_test.routing.yml +++ b/core/modules/system/tests/modules/render_attached_test/render_attached_test.routing.yml @@ -18,3 +18,24 @@ render_attached.feed_single: _controller: '\Drupal\render_attached_test\Controller\TestController::feed' requirements: _access: 'TRUE' + +render_attached.teapot_dpa: + path: '/render_attached_test/teapot_dpa' + defaults: + _controller: '\Drupal\render_attached_test\Controller\TestController::teapotHeaderStatusDpa' + requirements: + _access: 'TRUE' + +render_attached.head_dpa: + path: '/render_attached_test/head_dpa' + defaults: + _controller: '\Drupal\render_attached_test\Controller\TestController::headDpa' + requirements: + _access: 'TRUE' + +render_attached.feed_single_dpa: + path: '/render_attached_test/feed_dpa' + defaults: + _controller: '\Drupal\render_attached_test\Controller\TestController::feedDpa' + requirements: + _access: 'TRUE' diff --git a/core/modules/system/tests/modules/render_attached_test/src/Controller/TestController.php b/core/modules/system/tests/modules/render_attached_test/src/Controller/TestController.php index 66b1419..01898ef 100644 --- a/core/modules/system/tests/modules/render_attached_test/src/Controller/TestController.php +++ b/core/modules/system/tests/modules/render_attached_test/src/Controller/TestController.php @@ -7,8 +7,6 @@ namespace Drupal\render_attached_test\Controller; -use Symfony\Component\HttpFoundation\Response; - /** * Controller for various permutations of #attached in the render array. */ @@ -42,10 +40,59 @@ public function head() { return $render; } + /** + * Test attached feed rendering. + * + * @return array + * A render array using the 'feed' directive. + */ public function feed() { $render = []; $render['#attached']['feed'][] = ['test://url', 'Your RSS feed.']; return $render; } + /** + * Test special header and status code rendering as a side-effect. + * + * @return array + * A generic render array. + */ + public function teapotHeaderStatusDpa() { + $render = []; + $render['#attached']['http_header'][] = ['Status', "418 I'm a teapot."]; + \drupal_process_attached($render); + return ['#markup' => "I'm some markup here to fool the kernel into rendering this page."]; + } + + /** + * Test attached HTML head rendering as a side-effect. + * + * @return array + * A render array using the 'html_head' directive. + */ + public function headDpa() { + $render = []; + $render['#attached']['http_header'][] = ['X-Test-Teapot-Replace', 'This value gets replaced']; + $render['#attached']['http_header'][] = ['X-Test-Teapot-Replace', 'Teapot replaced', TRUE]; + $render['#attached']['http_header'][] = ['X-Test-Teapot-No-Replace', 'This value is not replaced']; + $render['#attached']['http_header'][] = ['X-Test-Teapot-No-Replace', 'This one is added', FALSE]; + $render['#attached']['http_header'][] = ['X-Test-Teapot', 'Teapot Mode Active']; + \drupal_process_attached($render); + return ['#markup' => "I'm some markup here to fool the kernel into rendering this page."]; + } + + /** + * Test attached feed rendering as a side-effect. + * + * @return array + * A render array using the 'feed' directive. + */ + public function feedDpa() { + $render = []; + $render['#attached']['feed'][] = ['test://url', 'Your RSS feed.']; + \drupal_process_attached($render); + return ['#markup' => "I'm some markup here to fool the kernel into rendering this page."]; + } + }