Commit 85145ba9 authored by catch's avatar catch

Issue #2350877 by dawehner, Wim Leers, hussainweb: Deprecate/rename...

Issue #2350877 by dawehner, Wim Leers, hussainweb: Deprecate/rename drupal_add_feed(), drupal_add_html_head(), drupal_add_html_head_link(), drupal_add_http_header(), and allow to be set declaratively in #attached.
parent 58b78c4b
......@@ -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.
......
......@@ -414,7 +414,7 @@ function drupal_page_get_cache(Request $request) {
* Use \Symfony\Component\HttpFoundation\Response->headers->set().
* See https://drupal.org/node/2181523.
*/
function drupal_add_http_header($name, $value, $append = FALSE) {
function _drupal_add_http_header($name, $value, $append = FALSE) {
// The headers as name/value pairs.
$headers = &drupal_static('drupal_http_headers', array());
......@@ -481,8 +481,8 @@ function _drupal_set_preferred_header_name($name = NULL) {
/**
* Sends the HTTP response headers that were previously set, adding defaults.
*
* Headers are set in drupal_add_http_header(). Default headers are not set
* if they have been replaced or unset using drupal_add_http_header().
* Headers are set in _drupal_add_http_header(). Default headers are not set
* if they have been replaced or unset using _drupal_add_http_header().
*
* @param array $default_headers
* (optional) An array of headers as name/value pairs.
......@@ -571,7 +571,7 @@ function drupal_page_header() {
*
* The headers allow as much as possible in proxies and browsers without any
* particular knowledge about the pages. Modules can override these headers
* using drupal_add_http_header().
* using _drupal_add_http_header().
*
* If the request is conditional (using If-Modified-Since and If-None-Match),
* and the conditions match those currently in the cache, a 304 Not Modified
......
......@@ -144,49 +144,6 @@
*/
const LOCALE_PLURAL_DELIMITER = "\03";
/**
* Adds content to a specified region.
*
* @param $region
* Page region the content is added to.
* @param $data
* Content to be added.
*/
function drupal_add_region_content($region = NULL, $data = NULL) {
static $content = array();
if (isset($region) && isset($data)) {
$content[$region][] = $data;
}
return $content;
}
/**
* Gets assigned content for a given region.
*
* @param $region
* A specified region to fetch content for. If NULL, all regions will be
* returned.
* @param $delimiter
* Content to be inserted between imploded array elements.
*/
function drupal_get_region_content($region = NULL, $delimiter = ' ') {
$content = drupal_add_region_content();
if (isset($region)) {
if (isset($content[$region]) && is_array($content[$region])) {
return implode($delimiter, $content[$region]);
}
}
else {
foreach (array_keys($content) as $region) {
if (is_array($content[$region])) {
$content[$region] = implode($delimiter, $content[$region]);
}
}
return $content;
}
}
/**
* Gets the name of the currently active installation profile.
*
......@@ -237,8 +194,11 @@ function drupal_get_profile() {
* An array of all stored HEAD elements.
*
* @see \Drupal\Core\Render\Element\HtmlTag::preRenderHtmlTag()
*
* @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)) {
......@@ -258,9 +218,12 @@ function drupal_add_html_head($data = NULL, $key = NULL) {
*
* @return string|array
* Return the rendered HTML head or the elements itself.
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* 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);
......@@ -279,8 +242,11 @@ function drupal_get_html_head($render = TRUE) {
* An internal system path or a fully qualified external URL of the feed.
* @param $title
* The title of the feed.
*
* @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)) {
......@@ -291,9 +257,12 @@ function drupal_add_feed($url = NULL, $title = '') {
/**
* Gets the feed URLs for the current page.
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* Use #attached on render arrays.
*/
function drupal_get_feeds() {
$feeds = drupal_add_feed();
$feeds = _drupal_add_feed();
return $feeds;
}
......@@ -906,8 +875,11 @@ function base_path() {
* Associative array of element attributes including 'href' and 'rel'.
* @param $header
* Optional flag to determine if a HTTP 'Link:' header should be sent.
*
* @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,
......@@ -918,10 +890,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);
}
/**
......@@ -1816,7 +1788,7 @@ function drupal_merge_attached(array $a, array $b) {
* other kind of attached data, the array key must be the full name of the
* callback function and each value an array of arguments. For example:
* @code
* $build['#attached']['drupal_add_http_header'] = array(
* $build['#attached']['http_header'] = array(
* array('Content-Type', 'application/rss+xml; charset=utf-8'),
* );
* @endcode
......@@ -1894,7 +1866,23 @@ 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 'html_head':
call_user_func_array('_drupal_add_html_head', $args);
break;
case 'feed':
call_user_func_array('_drupal_add_feed', $args);
break;
case 'html_head_link':
call_user_func_array('_drupal_add_html_head_link', $args);
break;
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));
}
}
}
......@@ -2055,6 +2043,9 @@ function drupal_process_states(&$elements) {
*
* @see \Drupal\Core\Asset\LibraryDiscovery
* @see hook_library_info_alter()
*
* @deprecated in Drupal 8.0.x, will be removed before Drupal 8.0.0
* Use #attached on render arrays.
*/
function _drupal_add_library($library_name, $every_page = NULL) {
$added = &drupal_static(__FUNCTION__, array());
......
......@@ -902,7 +902,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
......
......@@ -98,7 +98,7 @@ public function onRespond(FilterResponseEvent $event) {
// Attach globally-declared headers to the response object so that Symfony
// can send them for us correctly.
// @todo remove this once we have removed all drupal_add_http_header()
// @todo remove this once we have removed all _drupal_add_http_header()
// calls.
$headers = drupal_get_http_header();
foreach ($headers as $name => $value) {
......
......@@ -52,23 +52,23 @@ 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' => [],
'feed' => [],
'html_head' => [],
'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])));
}
// Add generic links from the page content.
foreach ($attached['drupal_add_html_head_link'] as $link) {
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
......
......@@ -173,7 +173,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;
}
......
......@@ -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,
);
......
......@@ -746,7 +746,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,
......
......@@ -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;
......
......@@ -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)),
......
......@@ -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)),
......
......@@ -366,7 +366,7 @@ function rdf_preprocess_user(&$variables) {
'lang' => '',
),
);
drupal_add_html_head($username_meta, 'rdf_user_username');
$variables['#attached']['html_head'][] = [$username_meta, 'rdf_user_username'];
}
}
}
......
......@@ -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)),
......
<?php
/**
* @file
* Definition of Drupal\system\Tests\Common\RegionContentTest.
*/
namespace Drupal\system\Tests\Common;
use Drupal\simpletest\WebTestBase;
/**
* Tests setting and retrieving content from theme regions.
*
* @group Common
*/
class RegionContentTest extends WebTestBase {
/**
* Tests setting and retrieving content for theme regions.
*/
function testRegions() {
$theme_key = \Drupal::theme()->getActiveTheme()->getName();
$block_regions = array_keys(system_region_list($theme_key));
$delimiter = $this->randomMachineName(32);
$values = array();
// Set some random content for each region available.
foreach ($block_regions as $region) {
$first_chunk = $this->randomMachineName(32);
drupal_add_region_content($region, $first_chunk);
$second_chunk = $this->randomMachineName(32);
drupal_add_region_content($region, $second_chunk);
// Store the expected result for a drupal_get_region_content call for this region.
$values[$region] = $first_chunk . $delimiter . $second_chunk;
}
// Ensure drupal_get_region_content returns expected results when fetching all regions.
$content = drupal_get_region_content(NULL, $delimiter);
foreach ($content as $region => $region_content) {
$this->assertEqual($region_content, $values[$region], format_string('@region region text verified when fetching all regions', array('@region' => $region)));
}
// Ensure drupal_get_region_content returns expected results when fetching a single region.
foreach ($block_regions as $region) {
$region_content = drupal_get_region_content($region, $delimiter);
$this->assertEqual($region_content, $values[$region], format_string('@region region text verified when fetching single region.', array('@region' => $region)));
}
}
}
......@@ -1181,4 +1181,20 @@ protected function randomContextValue() {
return $tokens[mt_rand(0, 4)];
}
/**
* Tests drupal_process_attached().
*/
public function testDrupalProcessAttached() {
// Specify invalid attachments in a render array.
$build['#attached']['library'][] = 'core/drupal.states';
$build['#attached']['drupal_process_states'][] = [];
try {
drupal_process_attached($build);
$this->fail("Invalid #attachment 'drupal_process_states' allowed");
}
catch (\Exception $e) {
$this->pass("Invalid #attachment 'drupal_process_states' not allowed");
}
}
}
......@@ -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)),
......
......@@ -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) {
......
......@@ -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 {
......
......@@ -44,11 +44,12 @@ 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 {
// Add the RSS icon to the view.
$feed_icon = array(
'#theme' => 'feed_icon',
'#url' => $url,
......@@ -56,8 +57,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(
// Attach a link to the RSS feed, which is an alternate representation.
$build['#attached']['html_head_link'][][] = array(
'rel' => 'alternate',
'type' => 'application/rss+xml',
'title' => $title,
......
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