2

有谁知道如何或可以指导我如何为当前节点的分类术语添加 body css 类?即<body class="term-dogs">“狗”是分类术语名称。它也可能只是术语 ID。无论哪种方式都很好,我只需要一个解决方案。这将用于 Drupal 7 zen 子主题

4

3 回答 3

2

这个答案花费的时间比我预期的要长。困难的部分是收集节点上的术语,因为与节点相关的所有分类函数都已删除或重构。最终,Pro Drupal 7 Development 的第 355 页用一个片段挽救了这一天,该片段完成了之前由 taxonomy_node_get_terms 处理的工作。

下面是对我有用的代码(查找“MAGIC BEGINS HERE”的部分)。假设您正在创建 Zen 的子主题,您需要将其移动到子主题的 template.php 文件中,并将其重命名为 YOURSUBTHEMENAME_preprocess_html:

/**
 * Override or insert variables into the html template.
 *
 * @param $vars
 *   An array of variables to pass to the theme template.
 * @param $hook
 *   The name of the template being rendered ("html" in this case.)
 */
function zen_preprocess_html(&$vars, $hook) {
  // If the user is silly and enables Zen as the theme, add some styles.
  if ($GLOBALS['theme'] == 'zen') {
    include_once './' . drupal_get_path('theme', 'zen') . '/zen-internals/template.zen.inc';
    _zen_preprocess_html($vars, $hook);
  }

  // Classes for body element. Allows advanced theming based on context
  // (home page, node of certain type, etc.)
  if (!$vars['is_front']) {
    // Add unique class for each page.
    $path = drupal_get_path_alias($_GET['q']);
    // Add unique class for each website section.
    list($section, ) = explode('/', $path, 2);
    if (arg(0) == 'node') {
      if (arg(1) == 'add') {
        $section = 'node-add';
      }
      elseif (is_numeric(arg(1)) && (arg(2) == 'edit' || arg(2) == 'delete')) {
        $section = 'node-' . arg(2);
      }
      // MAGIC BEGINS HERE
      $node = node_load(arg(1));
      $results = field_view_field('node', $node, 'field_tags', array('default'));
      foreach ($results as $key => $result) {
        if (is_numeric($key)) {
          $vars['classes_array'][] = strtolower($result['#title']);
        }
      }
      // MAGIC ENDS HERE
    }
    $vars['classes_array'][] = drupal_html_class('section-' . $section);
  }
  if (theme_get_setting('zen_wireframes')) {
    $vars['classes_array'][] = 'with-wireframes'; // Optionally add the wireframes style.
  }
  // Store the menu item since it has some useful information.
  $vars['menu_item'] = menu_get_item();
  switch ($vars['menu_item']['page_callback']) {
    case 'views_page':
      // Is this a Views page?
      $vars['classes_array'][] = 'page-views';
      break;
    case 'page_manager_page_execute':
    case 'page_manager_node_view':
    case 'page_manager_contact_site':
      // Is this a Panels page?
      $vars['classes_array'][] = 'page-panels';
      break;
  }
}
于 2011-01-10T07:53:02.957 回答
1

我需要知道如何做到这一点,而 Matt V 的解决方案非常有效。我对他的作品做了一些补充。我调用了替换空格和无效字符的 drupal_html_class。我在术语 ID 中添加了允许您定位术语,即使术语名称发生更改。

// MAGIC BEGINS HERE
$node = node_load(arg(1));
$results = field_view_field('node', $node, 'field_tags', array('default'));
foreach ($results as $key => $result) {
    if (is_numeric($key)) {
    // Call drupal_html_class to make safe for a css class (remove spaces, invalid characters)
    $vars['classes_array'][] = "taxonomy-" . strtolower(drupal_html_class( $result['#title']) );
    // Add taxonomy ID. This will allow targeting of the taxonomy class even if the title changes
    $vars['classes_array'][] = "taxonomy-id-" . $result['#options']['entity']->tid  ;
    }
}
// MAGIC ENDS HERE
于 2011-01-28T04:24:01.863 回答
0

不确定你对那个 body 标签的意思,但是节点上的类是在这里生成的:

http://api.drupal.org/api/drupal/modules--node--node.module/function/template_preprocess_node/7

您可以通过实现 yourmodule_preprocess_node($vars) 添加更多内容,然后将您想要的任何内容添加到 $vars['classes_array']

于 2011-01-10T05:57:05.027 回答