Как вы заказываете мета-теги, добавляемые drupal add html head()?


Я добавляю поддержку Open Graph на сайт Drupal, и у меня есть куча вызовов drupal_add_html_head(), например:

  $og_title = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title', 
      'content' => $node->title,
    ),
  );
  drupal_add_html_head($og_title, 'zujava_og_title');

 $og_url = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url', 
      'content' => url('node/' . $node->nid, array('absolute' => TRUE)),
    ),
  );
  drupal_add_html_head($og_url, 'zujava_og_url');

В общей сложности у меня их 10. Похоже, они выводятся не в том порядке, в котором они вызываются (все в одной функции).

Есть ли какой-то вес, который я могу использовать для установки порядка?

 12
Author: kiamlaluno, 2012-05-17

1 answers

Используйте свойство #вес. Поскольку drupal_get_html_head() использует drupal_render() для отображения мета-тегов, при их отображении используется #вес.

Я использую следующий код для выполнения теста на своем локальном сайте; это тот же код, который вы используете, за исключением того, что он не имеет никакой ссылки на объект узла.

  $og_title = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title', 
      'content' => "This is the title",
    ),
  );
  drupal_add_html_head($og_title, 'zujava_og_title');

 $og_url = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url', 
      'content' => url('node/1', array('absolute' => TRUE)),
    ),
  );
  drupal_add_html_head($og_url, 'zujava_og_url');

  dsm(drupal_get_html_head());

Результат, который я получил, следующий.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta property="og:url" content="http://tero.local/dr72/node/1" />
<meta name="Generator" content="Drupal 7 (http://drupal.org)" />
<meta property="og:title" content="This is the title" />

Как вы видите, последний добавленный тег появляется первым.

Затем я выполняю следующее код.

  $og_title = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:title', 
      'content' => "This is the title",
    ),
    '#weight' => 10,
  );
  drupal_add_html_head($og_title, 'zujava_og_title');

 $og_url = array(
    '#tag' => 'meta',
    '#attributes' => array(
      'property' => 'og:url', 
      'content' => url('node/1', array('absolute' => TRUE)),
    ),
    '#weight' => 200,
  );
  drupal_add_html_head($og_url, 'zujava_og_url');

  dsm(drupal_get_html_head());

Результат, который я получил, следующий.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="Generator" content="Drupal 7 (http://drupal.org)" />
<meta property="og:title" content="This is the title" />
<meta property="og:url" content="http://tero.local/dr72/node/1" />

Как вы видите, порядок метатегов был изменен; метатеги, добавленные из кода, появляются после метатегов по умолчанию, добавленных из Drupal.

_drupal_default_html_head() (функция, возвращающая мета-теги по умолчанию) использует #вес для мета-тега "Тип содержимого".

  $elements['system_meta_content_type'] = array(
    '#type' => 'html_tag', 
    '#tag' => 'meta', 
    '#attributes' => array(
      'http-equiv' => 'Content-Type', 
      'content' => 'text/html; charset=utf-8',
    ),
    // Security: This always has to be output first. 
    '#weight' => -1000,
  );
 15
Author: kiamlaluno, 2012-05-18 17:43:22