如何使用Drupal 7.x在“<head>”中添加一些东西


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

我非常怀疑...我添加内容的方式是正确的(例如发布商谷歌加的链接,元标记或外部资源 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');
}

我希望你能帮助我:)对不起我的英语

编辑我问这个也是因为这些是我的最新更改,现在我注意到如果我登录并看到我的网站Firefox没有 CSS !荒谬!

现在我有 3 种方法:

  1. 不正确如何在head中添加内容
  2. 问题火狐 (44.0.2)
  3. 问题模块 高级 CSS/JS 聚合

编辑 2.0 这是火狐的问题(我重置它并解决了问题)...但是我想知道这种添加head的方式是否正确;)

drupal_add_html_head将输出添加到 HTML 页面的 HEAD 标记中。只要不发送标头,就可以调用此函数。

更多信息请访问 https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_add_html_head/7

我发现最简单的方法是将以下内容放在我的模板中.php

    function THEMENAME_preprocess_html(&$variables) {
      if(drupal_is_front_page()) {
        $meta_description = array(
                '#type' => 'html_tag',
                '#tag' => 'meta',
                '#attributes' => array(
                    'name' => 'description',
                    'content' =>  'blah blah blah'
                )
        );
        $meta_keywords = array(
                '#type' => 'html_tag',
                '#tag' => 'meta',
                '#attributes' => array(
                    'name' => 'keywords',
                    'content' =>  'some, keywords'
                )
        );
        drupal_add_html_head( $meta_description, 'meta_description' );
        //drupal_add_html_head( $meta_keywords, 'meta_keywords' );
        drupal_add_html_head_link(array(
          'rel' => 'publisher',
          'href' => 'https://plus.google.com/xxxxxxxxx',
        ));
      }
    }

仅在首页上需要发布者,因此drupal_is_front_page