Trying to add an html head tag in Drupal 8, beta 2 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(&amp;$page) {
    $viewport = array(
          '#type' =&gt; 'html_tag',
          '#tag' =&gt; 'meta',
          '#attributes' =&gt; array(
            'name' =&gt; 'viewport',
            'content' =&gt; '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' =&gt; 'html_tag',
    '#tag' =&gt; 'meta',
    '#attributes' =&gt; array(
      'name' =&gt; 'viewport',
      'content' =&gt; '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 &amp;$page) {
    $viewport = array(
      '#type' =&gt; 'html_tag',
      '#tag' =&gt; 'meta',
      '#attributes' =&gt; array(
        'name' =&gt; 'viewport',
        'content' =&gt; '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?
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.