From f577bdcb33cea004bcc024bffd7b5ebbb877ec1c Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Mon, 8 Oct 2018 12:28:07 +0100 Subject: [PATCH] Issue #2566619 by claudiu.cristea, marvin_B8, andypost, alvar0hurtad0, Mile23, joshi.rohit100, dawehner, joelpittet: Deprecate drupal_http_header_attributes() --- core/includes/common.inc | 20 +++++----- .../HtmlResponseAttachmentsProcessor.php | 26 +++++++++++- .../Core/Render/RendererLegacyTest.php | 40 +++++++++++++++++++ 3 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php diff --git a/core/includes/common.inc b/core/includes/common.inc index 49cf6e104a7..183d6dbd585 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -14,6 +14,7 @@ use Drupal\Component\Utility\Html; use Drupal\Component\Utility\SortArray; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; +use Drupal\Core\Render\HtmlResponseAttachmentsProcessor; use Drupal\Core\Render\Element\Link; use Drupal\Core\Render\Markup; use Drupal\Core\StringTranslation\TranslatableMarkup; @@ -350,21 +351,22 @@ function date_iso8601($date) { /** * Formats an attribute string for an HTTP header. * - * @param $attributes + * @param array $attributes * An associative array of attributes such as 'rel'. * - * @return + * @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. + * + * @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 = []) { - foreach ($attributes as $attribute => &$data) { - if (is_array($data)) { - $data = implode(' ', $data); - } - $data = $attribute . '="' . $data . '"'; - } - return $attributes ? ' ' . implode('; ', $attributes) : ''; + @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); + return HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes($attributes); } /** diff --git a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php index b1e8fb0e768..36042c3c7c7 100644 --- a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php +++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php @@ -218,6 +218,30 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorIn 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']). * @@ -421,7 +445,7 @@ class HtmlResponseAttachmentsProcessor implements AttachmentsResponseProcessorIn // Also add a HTTP header "Link:". $href = '<' . Html::escape($attributes['href']) . '>'; unset($attributes['href']); - if ($param = drupal_http_header_attributes($attributes)) { + if ($param = static::formatHttpHeaderAttributes($attributes)) { $href .= ';' . $param; } diff --git a/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php new file mode 100644 index 00000000000..c2d7a67f91c --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Render/RendererLegacyTest.php @@ -0,0 +1,40 @@ +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']]], + ]; + } + +}