While fixing the image browse module for 4.5 I found that drupal_set_html_head cannot be called from everywhere. It (obviously) only works when called before the header is printed/generated by the theme. The image_browse module wats to set javascript in the header from the block hook (any other palce would mean iether using globals or double database calls.).

We should either document that this is the case, or otherwise change its behaviour. The attached pathc adds a line of documentation.

Comments

Uwe Hermann’s picture

dries’s picture

IRC, this can be called as long the headers aren't sent. Saying it must be called from _menu() or _init() doesn't sound right to me.

Bèr Kessels’s picture

Tis small pathc conatins your exact words "This can be called as long the headers aren't sent."

Bèr Kessels’s picture

StatusFileSize
new492 bytes

whoops.

dries’s picture

Committed to HEAD. Thanks.

Bèr Kessels’s picture

For now I am bringing this back to life. (setting it to active).
I beleive documenting a "bug" is certainly not similar to fixing it. We should really find a way to "fix" this issue. But I cannot yet think of a way.

It is technically impossible to add something to the head when its arlready printed. So we need another solution. I am thinking of some flag that you can set (e.g. in init) called "wait_with_print" that, if set TRUE will hold "theme_page" untill all functions are ran, and the _exit() hoos is going to be called. Just before calling that hook, we can print the theme_page.

I am not too sure about all this. But I /am/ sure that it makes nearly no sense to have a drupal_set_html_head, if you can hardly use it.

please mark this closed or wontfix if there are valid reasons for not thinking about a better solution.

Anonymous’s picture

First, we should discourage use of init hook since it defeats many of the benefits of the bootstrap. Please don't mention it here.

Second, Dries is right that this function call can appear anywhere. The reason to put it in hook_menu(!$may_cache) is to guarantee that it gets called exactly once per page (no matter which module is rendering the page). This is the desired behavior when injecting CSS and Javascript and so on.

Bèr Kessels’s picture

fixed with the new methot of centralised theme_page print

Anonymous’s picture