igorpavlov
igorpavlov

Reputation: 3626

How to override already added meta tags in Drupal 7?

In the theme vendor file I have:

$element = array(
  '#tag' => 'meta',
  '#attributes' => array(
    'name' => 'viewport',
    'content' => 'width=device-width, initial-scale=1.0',
  ),
);
drupal_add_html_head($element, 'bootstrap_responsive');

I do not want to touch that file, but I would like to override this setting somewhere else. I can of course do:

drupal_add_html_head(array(
  '#tag' => 'meta',
  '#attributes' => array(
    'name' => 'viewport',
    'content' => 'maximum-scale=1.0' // Change is here
  )
), 'bootstrap_responsive_2');

That will work but would also be pretty ugly since it will create 2 meta tags in the end.

<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="viewport" content="maximum-scale=1.0" />

Can I simply override the already added meta tag?

Upvotes: 2

Views: 478

Answers (1)

norman.lol
norman.lol

Reputation: 5374

From within a custom module implement hook_html_head_alter. Read the comments on that page behind the link, you'll find some useful snippets there.

function MYMODULE_html_head_alter(&$head_elements) {

  foreach ($head_elements as $key => $element) {

    if (isset($element['#attributes']['name']) && $element['#attributes']['name'] == 'viewport') {

      // Override 'content'.
      $head_elements[$key]['#attributes']['content'] = 'maximum-scale=1.0';
    }
  }
}

Upvotes: 3

Related Questions