Issue #2566619 by claudiu.cristea, marvin_B8, andypost, alvar0hurtad0, Mile23, joshi.rohit100, dawehner, joelpittet: Deprecate drupal_http_header_attributes()

8.7.x
Nathaniel Catchpole 2018-10-08 12:28:07 +01:00
parent d8a6a68bbf
commit f577bdcb33
3 changed files with 76 additions and 10 deletions

View File

@ -14,6 +14,7 @@ use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\SortArray; use Drupal\Component\Utility\SortArray;
use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\Core\Render\Element\Link; use Drupal\Core\Render\Element\Link;
use Drupal\Core\Render\Markup; use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup; use Drupal\Core\StringTranslation\TranslatableMarkup;
@ -350,21 +351,22 @@ function date_iso8601($date) {
/** /**
* Formats an attribute string for an HTTP header. * Formats an attribute string for an HTTP header.
* *
* @param $attributes * @param array $attributes
* An associative array of attributes such as 'rel'. * An associative array of attributes such as 'rel'.
* *
* @return * @return string
* A ; separated string ready for insertion in a HTTP header. No escaping is * A ; separated string ready for insertion in a HTTP header. No escaping is
* performed for HTML entities, so this string is not safe to be printed. * performed for HTML entities, so this string is not safe to be printed.
*
* @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use
* \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes()
* instead.
*
* @see https://www.drupal.org/node/3000051
*/ */
function drupal_http_header_attributes(array $attributes = []) { function drupal_http_header_attributes(array $attributes = []) {
foreach ($attributes as $attribute => &$data) { @trigger_error("drupal_http_header_attributes() is deprecated nn Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes() instead. See https://www.drupal.org/node/3000051", E_USER_DEPRECATED);
if (is_array($data)) { return HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes($attributes);
$data = implode(' ', $data);
}
$data = $attribute . '="' . $data . '"';
}
return $attributes ? ' ' . implode('; ', $attributes) : '';
} }
/** /**

View File

@ -218,6 +218,30 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorIn
return $response; return $response;
} }
/**
* Formats an attribute string for an HTTP header.
*
* @param array $attributes
* An associative array of attributes such as 'rel'.
*
* @return string
* A ; separated string ready for insertion in a HTTP header. No escaping is
* performed for HTML entities, so this string is not safe to be printed.
*
* @internal
*
* @see https://www.drupal.org/node/3000051
*/
public static function formatHttpHeaderAttributes(array $attributes = []) {
foreach ($attributes as $attribute => &$data) {
if (is_array($data)) {
$data = implode(' ', $data);
}
$data = $attribute . '="' . $data . '"';
}
return $attributes ? ' ' . implode('; ', $attributes) : '';
}
/** /**
* Renders placeholders (#attached['placeholders']). * Renders placeholders (#attached['placeholders']).
* *
@ -421,7 +445,7 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorIn
// Also add a HTTP header "Link:". // Also add a HTTP header "Link:".
$href = '<' . Html::escape($attributes['href']) . '>'; $href = '<' . Html::escape($attributes['href']) . '>';
unset($attributes['href']); unset($attributes['href']);
if ($param = drupal_http_header_attributes($attributes)) { if ($param = static::formatHttpHeaderAttributes($attributes)) {
$href .= ';' . $param; $href .= ';' . $param;
} }

View File

@ -0,0 +1,40 @@
<?php
namespace Drupal\KernelTests\Core\Render;
use Drupal\Core\Render\HtmlResponseAttachmentsProcessor;
use Drupal\KernelTests\KernelTestBase;
/**
* Deprecation tests cases for the render layer.
*
* @group legacy
*/
class RendererLegacyTest extends KernelTestBase {
/**
* Tests deprecation of the drupal_http_header_attributes() function.
*
* @dataProvider providerAttributes
*
* @expectedDeprecation drupal_http_header_attributes() is deprecated nn Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes() instead. See https://www.drupal.org/node/3000051
*/
public function testHeaderAttributes($expected, $attributes) {
$this->assertSame($expected, drupal_http_header_attributes($attributes));
$this->assertSame($expected, HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes($attributes));
}
/**
* Provides a list of attributes to test.
*/
public function providerAttributes() {
return [
[' foo=""', ['foo' => '']],
[' foo=""', ['foo' => []]],
[' foo="bar"', ['foo' => 'bar']],
[' foo="bar"', ['foo' => ['bar']]],
[' foo="bar baz"', ['foo' => ['bar', 'baz']]],
];
}
}