API page: https://api.drupal.org/api/drupal/core%21modules%21system%21theme.api.ph...

> An empty renderable array representing the page.

I doubt that it's empty in an alter hook!

Comments

jhodgdon’s picture

Title: hook_page_attachments_alter() does not document its $page parameter properly » hook_page_attachments() and _alter() hooks have incorrect docs
Issue tags: +Novice

Well, if you look at where the hook is invoked, actually what happens is:

a) hook_page_attachments() is called on each module that implements it. Starting with an empty array, the hook is invoked on each module, passing the result to the next module, to build up an array of attachments.

b) hook_page_attachments_alter() is then called on each module or theme that implements it. What is passed in is the result of hook_page_attachments().

c) If either of these hooks adds anything but #attached and #post_render_cache elements to the array, and exception is thrown.

So both of these hooks have incorrect documentation, and neither one mentions (c). The docs for both hooks need an update:

(1) Rather than $page they should call the parameter $attachments, and document that you should add elements to #attached and #post_render_cache elements only.

(2) In the alter hook docs, this sentence should be removed:

If you want to alter the attachments added by other modules or if your module depends on the elements of other modules, use hook_page_attachments_alter() instead, which runs after this hook.

This should be a good project for a novice contributor...

zealfire’s picture

Assigned: Unassigned » zealfire
Status: Active » Needs review
StatusFileSize
new2.34 KB

Thanks @jhodgdon for earlier helping me on irc.

jhodgdon’s picture

Status: Needs review » Needs work

Thanks for the patch! It's a great start... A few things to address:

a) In both hooks:

- * @param array &$page
+ * @param array &$attachments
  *   An empty renderable array representing the page.

I think we need to change the description here as well as the name of the parameter.

b) In both hooks:

+ * If you try to attach anything but #attached and #post_render_cache to the array
+ * an exception is thrown.

I don't think we should use the word "attach" here for the array? How about "If you try to add anything..."?

The rest looks great!

zealfire’s picture

Status: Needs work » Needs review
StatusFileSize
new2.43 KB

@jhodgdon, modified patch as directed by you.Thanks

jhodgdon’s picture

Looks good to me! I guess... Maybe in the _alter() hook, should we change the @param description to say:

Array of all attachments provided by hook_page_attachments() implementations.

?? Because it's an alter hook...

zealfire’s picture

StatusFileSize
new2.47 KB

@jhodgdon,didn't noticed that earlier.Thanks

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Great, thanks!

catch’s picture

Status: Reviewed & tested by the community » Needs review

Should this use @throws?

jhodgdon’s picture

I don't think so. @throws is usually for "This function itself throws an exception", not for "If you put the wrong thing into your hook implementation, the function that invokes this hook will throw an exception", right?

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Probably...

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Yep no @throws. Documentation is not frozen during beta. Committed 17c9a2a and pushed to 8.0.x. Thanks!

  • alexpott committed 17c9a2a on 8.0.x
    Issue #2401355 by zealfire: hook_page_attachments() and _alter() hooks...

Status: Fixed » Closed (fixed)

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