如何为当前节点添加主体类';Drupal 7主题的分类
有人知道或者可以指导我如何为当前节点的分类术语添加body css类吗?i、 e.如何为当前节点添加主体类';Drupal 7主题的分类,drupal,drupal-7,Drupal,Drupal 7,有人知道或者可以指导我如何为当前节点的分类术语添加body css类吗?i、 e.,其中“dogs”是分类术语名称。它也可能只是一个术语ID。无论哪种方式都可以,我只需要一个解决方案。这将用于Drupal 7 zen子主题不确定body标记的含义,但节点上的类在此处生成: 您可以通过实现模块预处理节点($vars)来添加更多内容,然后添加想要添加的任何内容$vars['classes\u array']这个答案比我预期的要长。最困难的部分是收集节点上的术语,因为。最终,Pro Drupal 7
,其中“dogs”是分类术语名称。它也可能只是一个术语ID。无论哪种方式都可以,我只需要一个解决方案。这将用于Drupal 7 zen子主题不确定body标记的含义,但节点上的类在此处生成:
您可以通过实现模块预处理节点($vars)来添加更多内容,然后添加想要添加的任何内容$vars['classes\u array']这个答案比我预期的要长。最困难的部分是收集节点上的术语,因为。最终,Pro Drupal 7开发的第355页通过一个代码片段挽救了这一天,该代码片段完成了之前由分类法\节点\获取\术语处理的工作 下面是对我有用的代码(请查找“魔法从这里开始”的部分)。假设您正在创建Zen的子主题,您将希望将其移动到子主题的template.php文件中,并将其重命名为YOURSUBTHEMENAME\u preprocess\u 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;
}
}
我需要知道如何做到这一点,Matt V的解决方案非常有效。我对他的工作做了一些补充。我调用了drupal_html_类来替换空格和无效字符。我在术语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
我没有把任何代码放在任何地方;)如果要添加类,请创建一个模块,实现上面提到的钩子,并执行类似于链接函数的操作。这已经是Drupal核心的一部分了。这太棒了。我现在正在考虑一个模块,该模块无需编码即可实现这一点。你知道,让Drupal世界成为下一代更美好的地方。我想补充的是,您还可以通过添加分类术语id或tag之类的类来实现这一点,其中是分类术语的实际术语id。当您使用单个术语id和使用Drupal接口翻译的术语名称来处理本地化的分类术语时,这将避免“Oopsies”。