Come aggiungere una classe corpo per la tassonomia del nodo corrente a un tema Drupal 7

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

  •  09-10-2019
  •  | 
  •  

Domanda

Qualcuno sa come o mi può guidare nella giusta direzione su come aggiungere una classe CSS per corpo termine tassonomia del nodo attuale? vale a dire <body class="term-dogs"> dove "cani" è il nome termine tassonomia. Potrebbe anche essere solo l'ID termine. In entrambi i casi è bene ho solo bisogno di una soluzione. Questo sarà per un Drupal 7 zen sottotema

È stato utile?

Soluzione

Questa risposta ha richiesto più tempo di quanto mi aspettassi da capire. La parte difficile raccoglieva le condizioni sul nodo, poiché Tutte le funzioni relative alla tassonomia nodi sono stati rimossi o refactoring . In definitiva, la pagina 355 di Pro Drupal 7 Sviluppo salvato la giornata con un frammento che fa il lavoro in precedenza gestito da taxonomy_node_get_terms.

Di seguito è riportato il codice che ha funzionato per me (look per la parte che dice "magia comincia qui"). Supponendo che si sta creando un sotto-tema dello Zen, ti consigliamo di spostare questo per il file template.php vostro di sotto-tema e rinominarlo in 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;
  }
}

Altri suggerimenti

avevo bisogno di sapere come fare questo e la soluzione di Matt V funzionato perfettamente. Ho fatto un paio di aggiunte al suo lavoro. Ho chiamato drupal_html_class che sostituisce gli spazi e caratteri non validi. E ho aggiunto l'ID termine per consentire di indirizzare un termine, anche se il nome dei cambiamenti termine.

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

Non sei sicuro di quello che vuoi dire con quel tag body, ma le classi sul nodo vengono generati qui:

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

È possibile aggiungere più implementando yourmodule_preprocess_node ($ VAR) e quindi aggiungere quello che vuoi a $ vars [ 'classes_array']

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top