Search code examples
phpdrupaldrupal-7head

How add something in "<head>" with Drupal 7.x?


i have big doubt... is correct how i add something (for example link for publisher google plus, or meta tag or external resource css):

function mysubtheme_page_alter($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')
 );
 drupal_add_html_head($viewport, 'viewport');


 $googleplus = array(
     '#type' => 'html_tag',
     '#tag' => 'link',
     '#attributes' => array(
        'href' =>  'https://plus.google.com/+google-plus',
        'rel' =>  'publisher')
 );
 drupal_add_html_head($googleplus, 'googleplus');


 $pinterest = array(
     '#type' => 'html_tag',
     '#tag' => 'meta',
     '#attributes' => array(
        'name' =>  'p:domain_verify',
        'content' =>  '7680eb52326ae9ee9e415d0ad')
 );
 drupal_add_html_head($pinterest, 'pinterest'); 


 $fontawesome = array(
     '#type' => 'html_tag',
     '#tag' => 'link',
     '#attributes' => array(
        'href' =>  '/sites/font-awesome.min.css',
        'rel' =>  'stylesheet')
 );
 drupal_add_html_head($fontawesome, 'fontawesome');
}

I hope you can help me :) sorry for my english

edit I ASK THIS also because those are my latest changes and now i noticed that if i logged and see my website with Firefox there aren't CSS ! ABSURD!

Now i have 3 ways:

  1. not correct how add something in head
  2. Issue Firefox (44.0.2)
  3. issue module ADVANCED CSS/JS AGGREGATION

EDIT 2.0 It was a problem with firefox (i reset it and issue was solved)... however i would like to know if this way to add in head is right ;)


Solution

  • drupal_add_html_head adds output to the HEAD tag of the HTML page. This function can be called as long as the headers aren't sent.

    More info at https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_html_head/7