有谁知道如何或可以指导我如何为当前节点的分类术语添加body css类?即<body class="term-dogs">
其中“狗”是分类学术语名称。它也可能只是术语ID。无论哪种方式都很好,我只需要一个解决方案。这将是Drupal 7 zen子主题
答案 0 :(得分:2)
这个答案的时间比我预期的要长。困难的部分是收集节点上的术语,因为All taxonomy functions relating to nodes have been removed or refactored。最终,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;
}
}
答案 1 :(得分: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
答案 2 :(得分:0)
不确定你对那个body标签的意思,但是节点上的类是在这里生成的:
http://api.drupal.org/api/drupal/modules--node--node.module/function/template_preprocess_node/7
您可以通过实现yourmodule_preprocess_node($ vars)添加更多内容,然后将您想要的任何内容添加到$ vars ['classes_array']