diff --git a/core/includes/batch.inc b/core/includes/batch.inc index 786ee3c..68fc14b 100644 --- a/core/includes/batch.inc +++ b/core/includes/batch.inc @@ -165,7 +165,7 @@ function _batch_progress_page() { '#message' => $message, '#label' => $label, '#attached' => array( - 'drupal_add_html_head' => array( + 'html_head' => array( array( array( // Redirect through a 'Refresh' meta tag if JavaScript is disabled. diff --git a/core/includes/common.inc b/core/includes/common.inc index 1307f5e..e2ef676 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -198,7 +198,7 @@ function drupal_get_profile() { * @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0 * Use #attached on render arrays. */ -function drupal_add_html_head($data = NULL, $key = NULL) { +function _drupal_add_html_head($data = NULL, $key = NULL) { $stored_head = &drupal_static(__FUNCTION__, array()); if (isset($data) && isset($key)) { @@ -223,7 +223,7 @@ function drupal_add_html_head($data = NULL, $key = NULL) { * Use #attached on render arrays. */ function drupal_get_html_head($render = TRUE) { - $elements = drupal_add_html_head(); + $elements = _drupal_add_html_head(); \Drupal::moduleHandler()->alter('html_head', $elements); if ($render) { return drupal_render($elements); @@ -246,7 +246,7 @@ function drupal_get_html_head($render = TRUE) { * @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0 * Use #attached on render arrays. */ -function drupal_add_feed($url = NULL, $title = '') { +function _drupal_add_feed($url = NULL, $title = '') { $stored_feed_links = &drupal_static(__FUNCTION__, array()); if (isset($url)) { @@ -262,7 +262,7 @@ function drupal_add_feed($url = NULL, $title = '') { * Use #attached on render arrays. */ function drupal_get_feeds() { - $feeds = drupal_add_feed(); + $feeds = _drupal_add_feed(); return $feeds; } @@ -881,7 +881,7 @@ function base_path() { * @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0 * Use #attached on render arrays. */ -function drupal_add_html_head_link($attributes, $header = FALSE) { +function _drupal_add_html_head_link($attributes, $header = FALSE) { $element = array( '#tag' => 'link', '#attributes' => $attributes, @@ -892,10 +892,10 @@ function drupal_add_html_head_link($attributes, $header = FALSE) { // Also add a HTTP header "Link:". $href = '<' . String::checkPlain($attributes['href']) . '>;'; unset($attributes['href']); - $element['#attached']['drupal_add_http_header'][] = array('Link', $href . drupal_http_header_attributes($attributes), TRUE); + $element['#attached']['http_header'][] = array('Link', $href . drupal_http_header_attributes($attributes), TRUE); } - drupal_add_html_head($element, 'drupal_add_html_head_link:' . $attributes['rel'] . ':' . $href); + _drupal_add_html_head($element, 'html_head_link:' . $attributes['rel'] . ':' . $href); } /** @@ -1868,7 +1868,27 @@ function drupal_process_attached($elements, $dependency_check = FALSE) { // special handling. foreach ($elements['#attached'] as $callback => $options) { foreach ($elements['#attached'][$callback] as $args) { - call_user_func_array($callback, $args); + // Limit the amount allowed entries. + switch ($callback) { + case '_drupal_add_html_head': + case 'html_head': + call_user_func_array('_drupal_add_html_head', $args); + break; + case '_drupal_add_feed': + case 'feed': + call_user_func_array('_drupal_add_feed', $args); + break; + case '_drupal_add_html_head_link': + case 'html_head_link': + call_user_func_array('_drupal_add_html_head_link', $args); + break; + case 'drupal_add_http_header': + case 'http_header': + call_user_func_array('drupal_add_http_header', $args); + break; + default: + throw new \LogicException(sprintf('You are not allowed to use %s in #attached', $callback)); + } } } diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 3c8d1216..7cfc943 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -904,7 +904,7 @@ function install_display_output($output, $install_state) { 'content' => 'noindex, nofollow', ), ); - drupal_add_html_head($noindex_meta_tag, 'install_meta_robots'); + $output['#attached']['html_head'][] = [$noindex_meta_tag, 'install_meta_robots']; // Only show the task list if there is an active task; otherwise, the page // request has ended before tasks have even been started, so there is nothing diff --git a/core/lib/Drupal/Core/Page/RenderHtmlRenderer.php b/core/lib/Drupal/Core/Page/RenderHtmlRenderer.php index f3c166c..9358e25 100644 --- a/core/lib/Drupal/Core/Page/RenderHtmlRenderer.php +++ b/core/lib/Drupal/Core/Page/RenderHtmlRenderer.php @@ -52,23 +52,32 @@ public function render(array $render_array) { $attached = isset($render_array['#attached']) ? $render_array['#attached'] : []; $attached += [ - 'drupal_add_feed' => [], - 'drupal_add_html_head' => [], - 'drupal_add_html_head_link' => [], + '_drupal_add_feed' => [], + 'feed' => [], + '_drupal_add_html_head' => [], + 'html_head' => [], + '_drupal_add_html_head_link' => [], + 'html_head_link' => [], ]; // Add feed links from the page content. - foreach ($attached['drupal_add_feed'] as $feed) { + foreach ($attached['feed'] as $feed) { + $fragment->addLinkElement(new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0]))); + } + foreach ($attached['_drupal_add_feed'] as $feed) { $fragment->addLinkElement(new FeedLinkElement($feed[1], $this->urlGenerator->generateFromPath($feed[0]))); } // Add generic links from the page content. - foreach ($attached['drupal_add_html_head_link'] as $link) { + foreach ($attached['_drupal_add_html_head_link'] as $link) { + $fragment->addLinkElement(new LinkElement($this->urlGenerator->generateFromPath($link[0]['href']), $link[0]['rel'])); + } + foreach ($attached['html_head_link'] as $link) { $fragment->addLinkElement(new LinkElement($this->urlGenerator->generateFromPath($link[0]['href']), $link[0]['rel'])); } - // @todo Also transfer the contents of "drupal_add_html_head" once + // @todo Also transfer the contents of "_drupal_add_html_head" once // https://www.drupal.org/node/2296951 lands. // @todo Transfer CSS and JS over to the fragment once those are supported diff --git a/core/modules/aggregator/src/Controller/AggregatorController.php b/core/modules/aggregator/src/Controller/AggregatorController.php index a37a81d..0cede69 100644 --- a/core/modules/aggregator/src/Controller/AggregatorController.php +++ b/core/modules/aggregator/src/Controller/AggregatorController.php @@ -176,7 +176,7 @@ public function adminOverview() { public function pageLast() { $items = $this->entityManager()->getStorage('aggregator_item')->loadAll(20); $build = $this->buildPageList($items); - $build['#attached']['drupal_add_feed'][] = array('aggregator/rss', $this->config('system.site')->get('name') . ' ' . $this->t('aggregator')); + $build['#attached']['feed'][] = array('aggregator/rss', $this->config('system.site')->get('name') . ' ' . $this->t('aggregator')); return $build; } diff --git a/core/modules/book/book.module b/core/modules/book/book.module index 6b631c7..8474f04 100644 --- a/core/modules/book/book.module +++ b/core/modules/book/book.module @@ -405,7 +405,7 @@ function template_preprocess_book_navigation(&$variables) { if ($prev = $book_outline->prevLink($book_link)) { $prev_href = \Drupal::url('entity.node.canonical', array('node' => $prev['nid'])); - $build['#attached']['drupal_add_html_head_link'][][] = array( + $build['#attached']['html_head_link'][][] = array( 'rel' => 'prev', 'href' => $prev_href, ); @@ -417,7 +417,7 @@ function template_preprocess_book_navigation(&$variables) { $book_manager = \Drupal::service('book.manager'); if ($book_link['pid'] && $parent = $book_manager->loadBookLink($book_link['pid'])) { $parent_href = \Drupal::url('entity.node.canonical', array('node' => $book_link['pid'])); - $build['#attached']['drupal_add_html_head_link'][][] = array( + $build['#attached']['html_head_link'][][] = array( 'rel' => 'up', 'href' => $parent_href, ); @@ -427,7 +427,7 @@ function template_preprocess_book_navigation(&$variables) { if ($next = $book_outline->nextLink($book_link)) { $next_href = \Drupal::url('entity.node.canonical', array('node' => $next['nid'])); - $build['#attached']['drupal_add_html_head_link'][][] = array( + $build['#attached']['html_head_link'][][] = array( 'rel' => 'next', 'href' => $next_href, ); diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index 102c67a..55a19ba 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -745,7 +745,7 @@ function content_translation_page_alter(&$page) { ->setOption('language', $language) ->setAbsolute() ->toString(); - $page['#attached']['drupal_add_html_head_link'][] = array( + $page['#attached']['html_head_link'][] = array( array( 'rel' => 'alternate', 'hreflang' => $language->id, diff --git a/core/modules/forum/src/Controller/ForumController.php b/core/modules/forum/src/Controller/ForumController.php index 5de156c..2ca2e8e 100644 --- a/core/modules/forum/src/Controller/ForumController.php +++ b/core/modules/forum/src/Controller/ForumController.php @@ -145,7 +145,7 @@ protected function build($forums, TermInterface $term, $topics = array(), $paren ); $build['#attached']['library'][] = 'forum/forum.index'; if (empty($term->forum_container->value)) { - $build['#attached']['drupal_add_feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); + $build['#attached']['feed'][] = array('taxonomy/term/' . $term->id() . '/feed', 'RSS - ' . $term->getName()); } return $build; diff --git a/core/modules/node/src/Controller/NodePreviewController.php b/core/modules/node/src/Controller/NodePreviewController.php index 35c65d9..2134abe 100644 --- a/core/modules/node/src/Controller/NodePreviewController.php +++ b/core/modules/node/src/Controller/NodePreviewController.php @@ -33,7 +33,7 @@ public function view(EntityInterface $node_preview, $view_mode_id = 'full', $lan foreach ($node_preview->uriRelationships() as $rel) { // Set the node path as the canonical URL to prevent duplicate content. - $build['#attached']['drupal_add_html_head_link'][] = array( + $build['#attached']['html_head_link'][] = array( array( 'rel' => $rel, 'href' => $node_preview->url($rel), @@ -42,7 +42,7 @@ public function view(EntityInterface $node_preview, $view_mode_id = 'full', $lan if ($rel == 'canonical') { // Set the non-aliased canonical path as a default shortlink. - $build['#attached']['drupal_add_html_head_link'][] = array( + $build['#attached']['html_head_link'][] = array( array( 'rel' => 'shortlink', 'href' => $node_preview->url($rel, array('alias' => TRUE)), diff --git a/core/modules/node/src/Controller/NodeViewController.php b/core/modules/node/src/Controller/NodeViewController.php index 84559a5..2e68391 100644 --- a/core/modules/node/src/Controller/NodeViewController.php +++ b/core/modules/node/src/Controller/NodeViewController.php @@ -27,7 +27,7 @@ public function view(EntityInterface $node, $view_mode = 'full', $langcode = NUL foreach ($node->uriRelationships() as $rel) { // Set the node path as the canonical URL to prevent duplicate content. - $build['#attached']['drupal_add_html_head_link'][] = array( + $build['#attached']['html_head_link'][] = array( array( 'rel' => $rel, 'href' => $node->url($rel), @@ -37,7 +37,7 @@ public function view(EntityInterface $node, $view_mode = 'full', $langcode = NUL if ($rel == 'canonical') { // Set the non-aliased canonical path as a default shortlink. - $build['#attached']['drupal_add_html_head_link'][] = array( + $build['#attached']['html_head_link'][] = array( array( 'rel' => 'shortlink', 'href' => $node->url($rel, array('alias' => TRUE)), diff --git a/core/modules/rdf/rdf.module b/core/modules/rdf/rdf.module index f407e83..4308e8e 100644 --- a/core/modules/rdf/rdf.module +++ b/core/modules/rdf/rdf.module @@ -367,7 +367,9 @@ function rdf_preprocess_user(&$variables) { 'lang' => '', ), ); - drupal_add_html_head($username_meta, 'rdf_user_username'); + // @todo Convert once https://www.drupal.org/node/2346369 is in. + $build['#attached']['html_head'][] = [$username_meta, 'rdf_user_username']; + drupal_render($build); } } } diff --git a/core/modules/system/src/Tests/Common/AddFeedTest.php b/core/modules/system/src/Tests/Common/AddFeedTest.php index a24581d..3b495b3 100644 --- a/core/modules/system/src/Tests/Common/AddFeedTest.php +++ b/core/modules/system/src/Tests/Common/AddFeedTest.php @@ -12,13 +12,13 @@ use Drupal\simpletest\WebTestBase; /** - * Make sure that drupal_add_feed() works correctly with various constructs. + * Make sure that _drupal_add_feed() works correctly with various constructs. * * @group Common */ class AddFeedTest extends WebTestBase { /** - * Tests drupal_add_feed() with paths, URLs, and titles. + * Tests _drupal_add_feed() with paths, URLs, and titles. */ function testBasicFeedAddNoTitle() { $path = $this->randomMachineName(12); @@ -29,10 +29,10 @@ function testBasicFeedAddNoTitle() { $external_for_title = 'http://' . $this->randomMachineName(12) . '/' . $this->randomMachineName(12); $fully_qualified_for_title = _url($this->randomMachineName(12), array('absolute' => TRUE)); - // Possible permutations of drupal_add_feed() to test. - // - 'input_url': the path passed to drupal_add_feed(), + // Possible permutations of _drupal_add_feed() to test. + // - 'input_url': the path passed to _drupal_add_feed(), // - 'output_url': the expected URL to be found in the header. - // - 'title' == the title of the feed as passed into drupal_add_feed(). + // - 'title' == the title of the feed as passed into _drupal_add_feed(). $urls = array( 'path without title' => array( 'url' => _url($path, array('absolute' => TRUE)), diff --git a/core/modules/system/src/Tests/Common/RenderTest.php b/core/modules/system/src/Tests/Common/RenderTest.php index d07085c..4a6e2ba 100644 --- a/core/modules/system/src/Tests/Common/RenderTest.php +++ b/core/modules/system/src/Tests/Common/RenderTest.php @@ -1155,4 +1155,20 @@ protected function randomContextValue() { return $tokens[mt_rand(0, 4)]; } + /** + * Tests drupal_process_attached(). + */ + public function testDrupalProcessAttached() { + // Specify an invalid render array. + $build['#attached']['library'][] = 'core/drupal.states'; + $build['#attached']['drupal_process_states'][] = []; + try { + drupal_process_attached($build); + $this->fail("Invalid #attachmend 'drupal_process_states' allowed"); + } + catch (\Exception $e) { + $this->pass("Invalid #attachmend 'drupal_process_states' not allowed"); + } + } + } diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index a715c0a..f17b105 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -110,7 +110,7 @@ function taxonomy_page_build(&$page) { if ($route_match->getRouteName() == 'entity.taxonomy_term.canonical' && ($term = $route_match->getParameter('taxonomy_term')) && $term instanceof TermInterface) { foreach ($term->uriRelationships() as $rel) { // Set the URI relationships, like canonical. - $page['#attached']['drupal_add_html_head_link'][] = array( + $page['#attached']['html_head_link'][] = array( array( 'rel' => $rel, 'href' => $term->url($rel), @@ -121,7 +121,7 @@ function taxonomy_page_build(&$page) { // Set the term path as the canonical URL to prevent duplicate content. if ($rel == 'canonical') { // Set the non-aliased canonical path as a default shortlink. - $page['#attached']['drupal_add_html_head_link'][] = array( + $page['#attached']['html_head_link'][] = array( array( 'rel' => 'shortlink', 'href' => $term->url($rel, array('alias' => TRUE)), diff --git a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php index 068197c..250ab84 100644 --- a/core/modules/views/src/Plugin/views/cache/CachePluginBase.php +++ b/core/modules/views/src/Plugin/views/cache/CachePluginBase.php @@ -231,10 +231,10 @@ public function postRender(&$output) { } * duplicate it. Later on, when gatherHeaders() is run, this information * will be removed so that we don't hold onto it. * - * @see drupal_add_html_head() + * @see _drupal_add_html_head() */ public function cacheStart() { - $this->storage['head'] = drupal_add_html_head(); + $this->storage['head'] = _drupal_add_html_head(); } /** @@ -246,7 +246,7 @@ public function cacheStart() { protected function gatherHeaders(array $render_array = []) { // Simple replacement for head if (isset($this->storage['head'])) { - $this->storage['head'] = str_replace($this->storage['head'], '', drupal_add_html_head()); + $this->storage['head'] = str_replace($this->storage['head'], '', _drupal_add_html_head()); } else { $this->storage['head'] = ''; @@ -261,7 +261,7 @@ protected function gatherHeaders(array $render_array = []) { */ public function restoreHeaders() { if (!empty($this->storage['head'])) { - drupal_add_html_head($this->storage['head']); + _drupal_add_html_head($this->storage['head']); } if (!empty($this->storage['css'])) { foreach ($this->storage['css'] as $args) { diff --git a/core/modules/views/src/Plugin/views/style/Opml.php b/core/modules/views/src/Plugin/views/style/Opml.php index ad23aae..229e4c2 100644 --- a/core/modules/views/src/Plugin/views/style/Opml.php +++ b/core/modules/views/src/Plugin/views/style/Opml.php @@ -44,7 +44,7 @@ public function attachTo(array &$build, $display_id, $path, $title) { $url = _url($this->view->getUrl(NULL, $path), $url_options); if ($display->hasPath()) { if (empty($this->preview)) { - $build['#attached']['drupal_add_feed'][] = array($url, $title); + $build['#attached']['feed'][] = array($url, $title); } } else { diff --git a/core/modules/views/src/Plugin/views/style/Rss.php b/core/modules/views/src/Plugin/views/style/Rss.php index 99dc0c3..366deae 100644 --- a/core/modules/views/src/Plugin/views/style/Rss.php +++ b/core/modules/views/src/Plugin/views/style/Rss.php @@ -44,8 +44,8 @@ public function attachTo(array &$build, $display_id, $path, $title) { $url = _url($this->view->getUrl(NULL, $path), $url_options); if ($display->hasPath()) { if (empty($this->preview)) { - // Add a call for drupal_add_feed to the view attached data. - $build['#attached']['drupal_add_feed'][] = array($url, $title); + // Add a call for _drupal_add_feed to the view attached data. + $build['#attached']['feed'][] = array($url, $title); } } else { @@ -56,8 +56,8 @@ public function attachTo(array &$build, $display_id, $path, $title) { ); $this->view->feed_icon = $feed_icon; - // Add a call for drupal_add_html_head_link to the view attached data. - $build['#attached']['drupal_add_html_head_link'][][] = array( + // Add the RSS icon to the view. + $build['#attached']['html_head_link'][][] = array( 'rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => $title,