Problem/Motivation

hook_page_build() and hook_page_alter() are deprecated, but still around for BC purposes. Unfortunately, they're broken.

Worse yet: they are broken in such a way that they break their successors: system_page_attachments() executed hook_page_build() and hook_page_alter(), and if there are any implementations of those BC hooks, the attachments added in system_page_build() and any hook_page_build() implementation executed before it (i.e. most of them) will be overwritten!

But, not only are they broken, everybody is wondering why we still have them. After all, they're still around for BC, but can't do the same things anymore, so it's actually quite misleading.

Proposed resolution

Remove them.

Remaining tasks

None.

User interface changes

None.

API changes

The already deprecated hooks hook_page_build() and hook_page_alter() are removed.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Wim Leers’s picture

Status: Active » Needs review
FileSize
8.05 KB
Wim Leers’s picture

Wim Leers’s picture

(Reported by berdir, discussed with catch, who really wanted the BC layer originally, but now agrees that we can remove it.)

catch’s picture

If we don't remove it, we need to 1. Fix it 2. Add tests for it.

Given this is just a function rename and the original issue was critical, I think adding the bc layer was a mistake.

Berdir’s picture

Status: Needs review » Reviewed & tested by the community

Agree with removing this...

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

We still have mentions of hook_page_build() in DefaultHtmlFragmentRenderer and DefaultHtmlPageRenderer.

hass’s picture

+++ /dev/null
@@ -1,44 +0,0 @@
-function bc_test_page_alter(&$page) {
-  $page['#attached']['library'][] = 'core/jquery';
-

This is no longer working in hook_page_attachments(). Where is the test for this?

Wim Leers’s picture

Issue summary: View changes
Priority: Major » Critical
Status: Needs work » Needs review
FileSize
10.23 KB
2.49 KB

#6: I intentionally didn't remove those mentions because it's not clear what to replace them with; we already removed "D7-style hook_page_build()", where you can add stuff to the page; HEAD's hook_page_build() can only add attachments. #2352155: Remove HtmlFragment/HtmlPage will remove DefaultHtmlFragmentRenderer and DefaultHtmlPageRenderer and hence fix this for us automatically. However, I understand your request, and I've complied with it as well as possible, given the transitional state we're in.

#7: You're right, test coverage for this is incomplete, which is why the IS says that the BC hook_page_build() and hook_page_alter() are broken. In the discussion until #5 (and in IRC), we've decided to remove hook_page_build() and hook_page_alter(), because they were misleading anyway.
Removing them also fixes the problem of them breaking hook_page_attachments() and hook_page_attachments_alter(): if there were any hook_page_build() or hook_page_alter() implementations, they would override (!!!) what all hook_page_attachments() and hook_page_attachments_alter() had added before and including in system.module's implementation.
Clarified that in the IS.

My apologies to all of you, I really dropped the ball on this one. Clearly I should have added more test coverage — even when like in this case I should've been able to extend existing test coverage, but there wasn't any. That's no excuse. Sorry!

hass’s picture

Status: Needs review » Needs work
+++ /dev/null
@@ -1,44 +0,0 @@
-function bc_test_page_alter(&$page) {
-  $page['#attached']['library'][] = 'core/jquery';
-

We need such a "library" test for hook_page_attachments() and hook_page_attachments_alter(). Please update the tests to run this with library. This is currently broken and I'm highly interested to see a test that verifies that this regression is fixed by this patch.

Berdir’s picture

Status: Needs work » Needs review

We figured this out, it does work.

hass’s picture

But we had tests for this and now we have no tests.

Wim Leers’s picture

No, we never had tests for attaching assets in hook_page_build(). I did not remove any tests. Look at the patch in #2350949: Add hook_page_attachments(_alter)() and deprecate hook_page_build/alter().

Tests now added.

hass’s picture

Patch in #8 removed the code in #9, but if this is added back now all is good.

catch’s picture

Status: Needs review » Reviewed & tested by the community

I shouldn't have asked for a bc layer in the first place, this is a big reason to differentiate between wrappers and layers when thinking about backwards compatibility. Thanks for the extra test coverage. RTBC.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work

It looks like hook_page_alter is still being fired in DefaultHtmlPageRenderer::renderPage()

    // Allow modules and themes to alter the page render array.
    // This allows e.g. themes to attach custom libraries.
    \Drupal::moduleHandler()->alter('page', $page_array);

    // @todo Move preparePage() before alter() above, so $page_array['#page'] is
    //   available in hook_page_alter(), so that HTML attributes can be altered.

Plus the comment is mentioning hook_page_alter.

Wim Leers’s picture

Status: Needs work » Postponed

I'd reroll this, but the remark in #15 is addressed by #2352155: Remove HtmlFragment/HtmlPage already, which is already RTBC. Trying to fix this here would be wasting time. Especially because if this'd land first, I'd have to reroll the other patch, which is 20 times bigger.

This will need to be rerolled once #2352155 lands.

catch’s picture

Status: Postponed » Needs work
Pinolo’s picture

Assigned: Unassigned » Pinolo
Pinolo’s picture

Issue tags: +#drupalsprintIT
Pinolo’s picture

Assigned: Pinolo » Unassigned
Status: Needs work » Needs review
FileSize
9.01 KB

Rerolled patch. Obtaining an interdiff turned out to be a PITA, so it's not here, sorry.

Wim Leers’s picture

Status: Needs review » Needs work

Thank you very much! Just one problem:

+++ b/core/modules/system/src/Tests/Common/PageRenderTest.php
@@ -33,24 +33,6 @@ function testHookPageAlter() {
@@ -61,7 +43,14 @@ function testHookPageAttachmentsAlter() {

@@ -61,7 +43,14 @@ function testHookPageAttachmentsAlter() {
   function assertPageRenderHookExceptions($module, $hook) {
     // Assert a valid hook implementation doesn't trigger an exception.
     $page = [];
-    drupal_prepare_page($page);
+    $page = drupal_prepare_page($page);
+    $expected = [
+      'library' => [
+        0 => 'core/foo',
+        2 => 'core/baz',
+      ],
+    ];
+    $this->assertEqual($page['#attached'], $expected);
 
     // Assert an invalid hook implementation doesn't trigger an exception.
     \Drupal::state()->set($module . '.' . $hook . '.descendant_attached', TRUE);

You're missing this hunk from #12.

Codenator’s picture

Assigned: Unassigned » Codenator

Going to re-roll from #12

Codenator’s picture

Apply patch resolve #21 and make re-roll from #12
My file patch to big so my re-roll is not right.
Going to re-roll soon again.

Codenator’s picture

Status: Needs work » Needs review
Codenator’s picture

Status: Needs review » Needs work

The last submitted patch, 23: rm_hook_page_build__hook_page_alter-2362987-23.patch, failed testing.

Codenator’s picture

Assigned: Codenator » Unassigned
Status: Needs work » Needs review
Codenator’s picture

@Wim Leers try to re-roll patch and find out drupal_prepare_page($page); removed from core so patch from #20 is ok

Pinolo’s picture

@codenator, @wim leers: exactly, I thought that hunk was superseded by the landing of #2352155: Remove HtmlFragment/HtmlPage

Wim Leers’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
9.01 KB

Haha, oops! And I'm the one who landed that patch! :P I just diffed my last patch and the one rerolled here, and that was the only hunk missing. I should've inspected that hunk more closely.

Reuploading the patch in #20. Thanks again, Pinolo! :)

RTBC per #14, Pinolo just rerolled it after #2352155: Remove HtmlFragment/HtmlPage landed and I just reuploaded his patch after my criticism was wrong.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

This issue is a critical task and is allowed per https://www.drupal.org/core/beta-changes. Committed b0f2463 and pushed to 8.0.x. Thanks!

We need to update the CR to reflect that hook_page_build and hook_page_alter are gone - https://www.drupal.org/node/2357755

  • alexpott committed b0f2463 on 8.0.x
    Issue #2362987 by Wim Leers, Codenator, Pinolo: Remove hook_page_build...
Wim Leers’s picture

#31: the CR was already updated in #2.

alexpott’s picture

re #33 well without looking at it I can tell the title has not been updated :)

Wim Leers’s picture

#34: HAH! Fixed.

Status: Fixed » Closed (fixed)

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