简体   繁体   中英

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?

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';
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM