I don't know when, but somewhere between beta 7 to 9 this has been broken I think. The html_head is no longer added to the page.
I require the same in reCAPTCHA and it is also not working inside this hook.
/**
* Implements hook_captcha().
*/
function recaptcha_captcha($op, $captcha_type = '') {
...
$build = [
'#attached' => [
'html_head' => [
[
[
'#tag' => 'script',
'#value' => Url::fromUri('https://www.google.com/recaptcha/api.js', ['query' => ['hl' => \Drupal::service('language_manager')->getCurrentLanguage()->getId()], 'absolute' => TRUE])->toString(),
'#attributes' => [
'defer' => TRUE,
'async' => TRUE,
],
],
'recaptcha_api',
],
],
],
];
\Drupal::service('renderer')->render($build);
...
Comments
Comment #1
hass commentedComment #2
hass commentedShould we use
drupal_process_attached($build);now fordrupal_render($build)???How many days may this work from now?
Comment #3
dawehnerThose bits are now rendered by bubbling up through the entire stack onto the final page array.
I think all you have to do is to manipulate
&$variables['#attached']...Comment #4
hass commentedThere is no
&$variablesin these hook. This is not a preprocess function in recaptcha.Comment #5
hass commentedComment #6
dawehnerWell, so if I read http://cgit.drupalcode.org/captcha/tree/image_captcha/image_captcha.module correctly, then you return basically a render array here, right?
Why can't you attach it there?
Comment #7
hass commentedInteresting question. Is it possible to attach html_head to a form element? Never tried this... not sure if captcha module need to do anything special than!?
Maybe I have not thought about this as drupal_add_js() is used in D6/D7.
Comment #8
catchYes.
Comment #9
hass commentedCool... I try this out later. Where can I find an example?
But what about other edge cases? In past catch said I should use drupal_render($build) and this is no longer working now. drupal_process_attached($build); seems working, but can I use this everywhere or will this also removed soon? You may not always have a preprocess function or form.
Comment #10
hass commentedThis attaching works great. Thanks for this useful hint.
But in situations where I do not have a form - who should this solved?
drupal_process_attached()? Why is drupal_render() no longer working?Comment #11
hass commenteddrupal_render() is broken.
And this does not bubble... the js is not added to the head for sure.
does not work either:
Works for unknown reasons:
Comment #12
andypostLooks there's no one (right) way to attach head links, also check related issue #2477223: Refactor _drupal_add_html_head, drupal_get_html_head, _drupal_add_html_head_link into the attachments processor, remove from common.inc.
Comment #13
mile23Any help turning these fails into tests on #2477223: Refactor _drupal_add_html_head, drupal_get_html_head, _drupal_add_html_head_link into the attachments processor, remove from common.inc. would be appreciated.
Also, you want the 'bare_html_page_renderer' service instead of 'renderer' if you must render something away from a controller.
Also you can still use
hook_html_head_alter(), although its fate has yet to be determined: #2555069: Remove invocation of hook_html_head_alter()Comment #14
hass commentedThis issue here may be solved by #2351015: URL generation does not bubble cache contexts
Comment #15
hass commentedComment #16
hass commentedComment #17
wim leersThe problems described in #11 can no longer be reproduced in HEAD, right? The first example in #11 is what is supported.
Comment #18
hass commentedBut the second example should also work I guess? How can this solved in situations where I do not have the bubbling up? Not sure if this still exists somewhere else...?
Comment #19
hass commented