How to add a body class for the current node's taxonomy to a Drupal 7 theme

StackOverflow https://stackoverflow.com/questions/4642561

  •  09-10-2019
  •  | 
  •  

سؤال

Does anyone know how or can guide me in the right direction on how to add a body css class for the current node's taxonomy term? i.e. <body class="term-dogs"> where "dogs" is the taxonomy term name. It could also be just the term ID. Either way is fine I just need a solution. This will be for a Drupal 7 zen sub-theme

هل كانت مفيدة؟

المحلول

This answer took longer than I expected to figure out. The hard part was collecting the terms on the node, since All taxonomy functions relating to nodes have been removed or refactored. Ultimately, page 355 of Pro Drupal 7 Development saved the day with a snippet that does the job previously handled by taxonomy_node_get_terms.

Below is the code that worked for me (look for the part that says "MAGIC BEGINS HERE"). Assuming you're creating a sub-theme of Zen, you'll want to move this to your sub-theme's template.php file and rename it to 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;
  }
}

نصائح أخرى

I needed to know how to do this and Matt V's solution worked perfectly. I made a couple of additions to his work. I called drupal_html_class which replaces spaces and invalid characters. And I added in the term ID to allow you to target a term even if the name of the term changes.

// 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

Not sure what you mean with that body tag, but the classes on the node are generated here:

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

You can add more by implementing yourmodule_preprocess_node($vars) and then add whatever you want to $vars['classes_array']

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top