9

With Drupal 8.0 beta2, drupal_add_html_head() has been deprecated in favor of #attached. So the old way was:

function MYTHEME_page_build(&$page) {
    $viewport = array(
          '#type' => 'html_tag',
          '#tag' => 'meta',
          '#attributes' => array(
            'name' => 'viewport',
            'content' => 'width=device-width, initial-scale=1.0, maximum-scale=2.0, minimum-scale=0.55, user-scalable=yes',
          ),
        );
        drupal_add_html_head($viewport, 'viewport');
}

Based on the change notice you now use #attached. So the code above is essentially the same with the last part changing to:

$build['#attached']['html_head'][] = [$viewport, 'viewport'];

This does not work though and no error messages in watchdog. I've also tried putting this in a page alter as well as swapping $build with $variables and $output but it still does not work. In addtion, I tried the entire thing as an array:

  $build['#attached']['html_head'][] = array(
    '#type' => 'html_tag',
    '#tag' => 'meta',
    '#attributes' => array(
      'name' => 'viewport',
      'content' => 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no',
    ),
  );

... but that did not work either. I am not sure what I am doing wrong, seems like it should be working.

Update

Here is working code which uses hook_page_attachments_alter which is not in D8 Beta 2 as it was just committed to dev.

function MYTHEME_page_attachments_alter(array &$page) {
    $viewport = array(
      '#type' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
        'name' => 'viewport',
        'content' => 'width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no',
      ),
    );
    $page['#attached']['html_head'][] = [$viewport, 'viewport'];
  }

The only issue now is that core's viewport tag does not get overwritten so I am stuck with my custom tag and core's. Not sure how to handle that one or should my tag be overwriting core's?

enter image description here

7

You don't have $build in preprocess, so can't work like that. $build needs to be a render array that is rendered somewhere. I also don't see how this could have worked before, as this is already converted in template_preprocess_html() before your function runs.

Add hook_page_attachments(_alter)() and deprecate hook_page_build/alter() was just committed, and as a theme, you can implement hook_page_attachments_alter(), look for examples in the committed patch, like common_test_page_attachments_alter().

So, something like $page['#attached']['html_head'][] = [$viewport, 'viewport']; should work in there.

  • Well, thanks for your help but I am still lost. I'd be curious to see if you can get this to work. I've tried hook_page_build and alter but still no joy. I even grabbed some examples from core with no luck. It doesn't seem like it should be that hard. – Danny Englander Oct 18 '14 at 15:16
  • 1
    Update your question with your current code examples then. Surely the example I pointed to is working , as it is used for tests. So you must be doing something differently. The hook I suggested to use is hook_page_attachments_alter(), not page_build(). When implementing a hook and something is not working, the first thing to verify is always if your code is even called, by adding an exit; for example. – Berdir Oct 18 '14 at 23:08
  • I've added working code which uses your suggested hook_page_attachments_alter. There is one side affect that I now have two viewport tags, one from core and the one I've added from my theme so I am not sure how to deal with that. – Danny Englander Oct 19 '14 at 0:35
  • 1
    That is what the second argument is supposed to be for, html_head elements are using that to be able to keep things unique. Looking at core, that element is added in DefaultHtmlFragementRenderer::setDefaultMetatags(). The new API there does not have a way for specify a key, which seems like a pretty serious issue. I suggest you open an issue for this. Maybe drupal.org/node/2350943 is related. – Berdir Oct 19 '14 at 9:30
  • I opened a core issue: drupal.org/node/2359987 – Danny Englander Oct 21 '14 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.