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

hass’s picture

Issue summary: View changes
hass’s picture

Should we use drupal_process_attached($build); now for drupal_render($build)???

How many days may this work from now?

dawehner’s picture

Those 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'] ...

hass’s picture

There is no &$variables in these hook. This is not a preprocess function in recaptcha.

hass’s picture

Issue summary: View changes
dawehner’s picture

Well, 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?

hass’s picture

Interesting 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.

catch’s picture

Category: Bug report » Support request
Priority: Critical » Normal

Is it possible to attach html_head to a form element?

Yes.

hass’s picture

Cool... 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.

hass’s picture

This 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?

hass’s picture

Category: Support request » Bug report

drupal_render() is broken.

And this does not bubble... the js is not added to the head for sure.

function example_preprocess_item_list(&$variables) {
  $variables['#attached']['html_head'][] = [
      [
        '#tag' => 'script',
        '#value' => 'window.test = true;',
      ],
      'example_script'
    ];
}

does not work either:

function example_preprocess_item_list(&$variables) {
  $page['#attached']['html_head'][] = [
      [
        '#tag' => 'script',
        '#value' => 'window.test = true;',
      ],
      'example_script'
    ];
}
\Drupal::service('renderer')->render($page);

Works for unknown reasons:

function example_preprocess_item_list(&$variables) {
  $page['#attached']['html_head'][] = [
      [
        '#tag' => 'script',
        '#value' => 'window.test = true;',
      ],
      'example_script'
    ];
    drupal_process_attached($page);
}
mile23’s picture

Any 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()

hass’s picture

hass’s picture

hass’s picture

wim leers’s picture

The problems described in #11 can no longer be reproduced in HEAD, right? The first example in #11 is what is supported.

hass’s picture

But 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...?

hass’s picture

Status: Active » Closed (duplicate)