Problem/Motivation

In #2477223: Refactor _drupal_add_html_head, drupal_get_html_head, _drupal_add_html_head_link into the attachments processor, remove from common.inc. we refactored _drupal_add_html_head_link() into the HtmlResponseAttachmentsProcessor class.

_drupal_add_html_head_link() is the only code that calls drupal_http_header_attributes().

Also, all drupal_http_header_attributes() does is to format headers using string manipulation.

Thus, we could fold drupal_http_header_attributes()'s behavior into existing methods within HtmlResponseAttachmentsProcessor.

OR...

We could refactor drupal_http_header_attributes() to be a helper method on HtmlResponseAttachmentsProcessor, since that's the only place its used.

Proposed resolution

  1. Refactor the behavior of drupal_http_header_attributes() to be a method on HtmlResponseAttachmentsProcessor.
  2. Write unit tests which prove its behavior is correct.

Remaining tasks

User interface changes

API changes

Data model changes

Beta phase evaluation

Reference: https://www.drupal.org/core/beta-changes
Issue category Task because it's a refactoring of code that works.
Issue priority Normal because it's inelegant.
Prioritized changes Prioritized for refactoring a zombie function into its proper scope and testability.
Disruption Not disruptive because nothing else calls this function.

Comments

Mile23 created an issue. See original summary.

mile23’s picture

Status: Active » Needs review
StatusFileSize
new977 bytes

This patch marks drupal_http_header_attributes() as deprecated for removal before the 8.0.0 release.

andypost’s picture

mile23’s picture

mile23’s picture

Title: Deprecate drupal_http_header_attributes() for removal before Drupal 8.0.0 » Remove dead code drupal_http_header_attributes()
Category: Task » Bug report
Issue summary: View changes
Status: Needs review » Needs work
Issue tags: +Novice

#2477223: Refactor _drupal_add_html_head, drupal_get_html_head, _drupal_add_html_head_link into the attachments processor, remove from common.inc. is in, so this is now dead code.

Ignore the previous patch and just remove drupal_http_header_attributes().

joshi.rohit100’s picture

Assigned: Unassigned » joshi.rohit100

Okay!

joshi.rohit100’s picture

Status: Needs work » Reviewed & tested by the community
StatusFileSize
new980 bytes

removed!

joshi.rohit100’s picture

Status: Reviewed & tested by the community » Needs review

sorry! wrong status

joshi.rohit100’s picture

sorry! wrong status

Status: Needs review » Needs work

The last submitted patch, 7: 2566619-7.patch, failed testing.

The last submitted patch, 7: 2566619-7.patch, failed testing.

joshi.rohit100’s picture

It seems like this function is still in use

andypost’s picture

Yep

$ git grep drupal_http_header_attributes
core/includes/common.inc:372:function drupal_http_header_attributes(array $attributes = array()) {
core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php:423:        $attached['http_header'][] = ['Link', $href . drupal_http_header_attributes($attributes), TRUE];
mile23’s picture

Status: Needs work » Postponed
Related issues: +#2571427: HtmlResponseAttachmentsProcessor tests misplaced during reroll

Indeed. It's not actually dead code.

Also, the tests were misplaced in a reroll: #2571427: HtmlResponseAttachmentsProcessor tests misplaced during reroll

So I'd suggest postponing this until after the tests are added back, because any refactoring here should add to those tests.

mile23’s picture

Status: Postponed » Needs work
joshi.rohit100’s picture

Assigned: joshi.rohit100 » Unassigned

Status: Needs work » Needs review

daffie queued 7: 2566619-7.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 7: 2566619-7.patch, failed testing.

daffie’s picture

Issue tags: +Needs reroll

Status: Needs work » Needs review

andypost queued 7: 2566619-7.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 7: 2566619-7.patch, failed testing.

sdstyles’s picture

Status: Needs work » Needs review
StatusFileSize
new980 bytes

Reroll #7

Status: Needs review » Needs work

The last submitted patch, 22: 2566619-22.patch, failed testing.

The last submitted patch, 22: 2566619-22.patch, failed testing.

sdstyles’s picture

drupal_http_header_attributes() is still called by HtmlResponseAttachmentsProcessor::processHtmlHeadLink()

mile23’s picture

Title: Remove dead code drupal_http_header_attributes() » Refactor drupal_http_header_attributes() into HtmlResponseAttachmentsProcessor
Category: Bug report » Task
Issue summary: View changes
marvin_b8’s picture

StatusFileSize
new2.49 KB

hi,
What should be tested?
it's a protected method.

marvin_b8’s picture

Status: Needs work » Needs review
mile23’s picture

Status: Needs review » Needs work

Thanks.

+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -420,11 +420,31 @@ protected function processHtmlHeadLink(array $html_head_link) {
+  protected function drupalHttpHeaderAttributes(array $attributes = array()) {

Let's rename it formatHttpHeaderAttributes(). 'Drupal' is superfluous.

Also a unit test would let us trust what it does.

Thus we need a unit test where we mock HtmlResponseAttachmentsProcessor and then use reflection to make formatHttpHeaderAttributes() accessible. Use a @dataProvider to feed in various inputs for $attributes along with an expected result.

alvar0hurtad0’s picture

Issue tags: -Needs reroll
StatusFileSize
new1.54 KB
new2.57 KB

Here is the patch in #27 with the fixes on #29 but the mock on 3th paragraph.

mile23’s picture

Status: Needs work » Needs review

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

joelpittet’s picture

Version: 8.2.x-dev » 8.3.x-dev
Status: Needs review » Needs work
Issue tags: +Dublin2016
+++ b/core/includes/common.inc
@@ -361,26 +361,6 @@ function date_iso8601($date) {
-function drupal_http_header_attributes(array $attributes = array()) {
-  foreach ($attributes as $attribute => &$data) {
-    if (is_array($data)) {
-      $data = implode(' ', $data);
-    }
-    $data = $attribute . '="' . $data . '"';
-  }
-  return $attributes ? ' ' . implode('; ', $attributes) : '';
-}

We will need keep this function with @deprecated but it can just be a wrapper for a class method.

marvin_b8’s picture

StatusFileSize
new2.17 KB

A wrapper is not possible because the method is protected
and i'm not sure if it make sense to change this.

reroll #30

marvin_b8’s picture

Status: Needs work » Needs review
joelpittet’s picture

Having a duplicate code in core will likely cause fragmentation if there is a bug fix and not remembered(we have one of these issues already in core with render escaping @see theme_render_and_autoescape()). If we can't make that method public for whatever reason, might as well make a BC public method that returns the result and marked as @internal or something.

dawehner’s picture

+++ b/core/includes/common.inc
@@ -351,6 +351,8 @@ function date_iso8601($date) {
+ * @deprecated in Drupal 8.0.0, will be removed before Drupal 9.0.0.
...
 function drupal_http_header_attributes(array $attributes = array()) {

+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -417,13 +417,33 @@ protected function processHtmlHeadLink(array $html_head_link) {
   /**
+   * Formats an attribute string for an HTTP header.
+   *
+   * @param $attributes
+   *   An associative array of attributes such as 'rel'.
+   *
+   * @return
+   *   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.
+   */
+  protected function formatHttpHeaderAttributes(array $attributes = array()) {

What about the following: a) Use 8.3.x here b) make the method public static but call it @internal and @deprecated, as in 9.x we can call to it internally

marvin_b8’s picture

StatusFileSize
new2.24 KB
new2.92 KB
dawehner’s picture

  1. +++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
    @@ -421,13 +421,37 @@ protected function processHtmlHeadLink(array $html_head_link) {
    +   *
    +   * @param $attributes
    +   *   An associative array of attributes such as 'rel'.
    +   *
    +   * @return
    +   *   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 Used by processHtmlHeadLink. Should not be used
    +   *           in user code.
    +   * @deprecated in Drupal 8.3.x, will be removed before Drupal 9.0.
    

    Is there a reason we don't just copy the existing documentation?

  2. +++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
    @@ -421,13 +421,37 @@ protected function processHtmlHeadLink(array $html_head_link) {
    +   * @param $attributes
    ...
    +   * @return
    

    Should be @return string and @param array attributes

  3. +++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
    @@ -421,13 +421,37 @@ protected function processHtmlHeadLink(array $html_head_link) {
    +   * @deprecated in Drupal 8.3.x, will be removed before Drupal 9.0.
    

    Maybe explain why its protected here.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

claudiu.cristea’s picture

Title: Refactor drupal_http_header_attributes() into HtmlResponseAttachmentsProcessor » Deprecate drupal_http_header_attributes()
Issue tags: -Novice, -Dublin2016 +Kill includes
StatusFileSize
new3.16 KB
new2.31 KB

Rerolled, adapted to the Drupal deprecation policy and added CR.

claudiu.cristea’s picture

I we want to keep the new method protected, we can add a deprecated public static method that just wraps the protected. But I think we can keep it public.

claudiu.cristea’s picture

Issue tags: +Needs issue summary update
StatusFileSize
new667 bytes
new3.17 KB

Fix a coding standards issue.

andypost’s picture

Looks ready, only question about docs & interface

+++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
@@ -431,6 +431,26 @@ protected function processHtmlHeadLink(array $html_head_link) {
+   * Formats an attribute string for an HTTP header.
...
+  public static function formatHttpHeaderAttributes(array $attributes = []) {

Maybe move documentation to \Drupal\Core\Render\AttachmentsResponseProcessorInterface

claudiu.cristea’s picture

Moved.

claudiu.cristea’s picture

StatusFileSize
new3.7 KB
new2.58 KB

Oh, the patch :)

Status: Needs review » Needs work

The last submitted patch, 50: 2566619-49.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 50: 2566619-49.patch, failed testing. View results

claudiu.cristea’s picture

Status: Needs work » Needs review
longwave’s picture

Status: Needs review » Needs work
+++ b/core/lib/Drupal/Core/Render/AttachmentsResponseProcessorInterface.php
@@ -51,4 +51,16 @@
+  public static function formatHttpHeaderAttributes(array $attributes = []);

Drupal\Core\Ajax\AjaxResponseAttachmentsProcessor implements this interface but doesn't implement this method. As this is a static I don't think it needs to be on the interface, it can just be declared on the class only?

andypost’s picture

+++ b/core/includes/common.inc
@@ -356,15 +357,16 @@ function date_iso8601($date) {
+  @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);

makes sense to add test to make sure this error is triggered

andypost’s picture

Status: Needs work » Needs review
StatusFileSize
new4.75 KB
new4.97 KB

Patch
- removes bethod from interface & marks @internal
- fix deprecated message
- adds test coverage

longwave’s picture

Status: Needs review » Reviewed & tested by the community

Looks good!

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed f577bdc and pushed to 8.7.x. Thanks!

  • catch committed f577bdc on 8.7.x
    Issue #2566619 by claudiu.cristea, marvin_B8, andypost, alvar0hurtad0,...
anavarre’s picture

+++ b/core/includes/common.inc
@@ -350,21 +351,22 @@ function date_iso8601($date) {
+  @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);

We made a typo here FYI - s/nn/in/

andypost’s picture

StatusFileSize
new2.06 KB

Fix typo

claudiu.cristea’s picture

Status: Fixed » Reviewed & tested by the community

  • catch committed 398885e on 8.7.x
    Issue #2566619 by claudiu.cristea, marvin_B8, andypost, alvar0hurtad0,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Patch was already in, but made the same change locally and committed it.

andypost’s picture

CR published

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.