6

I find it a bit disturbing that you cannot set which fields of taxonomy terms are displayed when you add a new node and have the terms listed.

There will be a bunch of terms which the user has to select one from. However, sometimes only showing a terms title wont be enough, so I'd like to also show a terms description when adding a new node.

How can you do that?

  • Requirement is not clear. Where you want to show the description? You want to show term description instead of term name in term selection select box field? – ARUN Nov 14 '13 at 9:51
  • 1
    thanks for your comment. imagine creating a node which can have categories. you have a radio list of all terms of a vocabulary. but sometimes, just showing the terms titles isnt satisfying enough, since terms can have descritions, too. so showing additional fields would be great – Alex Nov 14 '13 at 11:02
2

The following solution slightly alters your requirement. Instead of having the term description field showing up as part of the term select widget, create a view block set to be visible on node/add pages which provides a table listing of each term and its description field.

To do this, create a new view of type "Taxonomy." It will have two fields: taxonomy term, and taxonomy term definition.

You could put help text associated with term_reference field which alerts the user to the block which holds the terms and description.

It would be very difficult to alter any select widget to include the description field in a way that wouldn't make a mess of the page.

  • Appreciate this as a solution, but as is all too typical with Drupal, it's a workaround. And a laborious one at that. What is the point in term descriptions if one cannot display them on the form?? – Matt Saunders Jan 22 '16 at 20:20
1

To show the taxonomy description instead of the name on a single node page you can use this in your theme template.php file:

function mytheme_preprocess_field(&$variables) {
  if ($variables['element']['#field_name'] == 'field_taxonomyfieldname') {
    if (isset($variables['element']['#items'][0]['taxonomy_term']->description)) {
      $variables['items'][0]['#markup'] = $variables['element']['#items'][0]['taxonomy_term']->description;
    }
  }
}

Making sure to change mytheme, field_taxonomyfieldname to your own theme and field names and then clear Drupal caches before testing.

0

You can use the term reference tree module, this gives you a taxonomy term theme function. Create a subtheme of seven and override the theme function in template.php. In the example below I am also using the tipsy module, which converts the .tipsy class into a tooltip, so the taxonomy term description appears after clicking or hovering on the tooltip on the node add form, but you could of course also just print the $description.

/**
 * This function prints a single item in the tree, followed by that item's children
 * (which may be another checkbox_tree_level).
 */
function seven_subtheme_checkbox_tree_item($variables) {
    $element = $variables['element'];
    $children = element_children($element);
    $output = "";

    $sm = $element['#level_start_minimized'] ? ' term-reference-tree-collapsed' : '';

    if (is_array($children) && count($children) > 1) {
      $output .= "<div class='term-reference-tree-button$sm'></div>";
    }
    elseif (!$element['#leaves_only']) {
      $output .= "<div class='no-term-reference-tree-button'></div>";
    }

    foreach ($children as $child) {
      // $child is the tid
      $term = taxonomy_term_load($child);
      $description = trim(strip_tags($term->description));
      if (!empty($description)) {
        $tooltip = l('ⓘ', '#', array(
          'attributes' => array(
            'class' => array('tipsy'),
            'title' => $description,
          )
        ));
      }
      $output .= drupal_render($element[$child]) . $tooltip;
    }

    return $output;
}
-1

a possible solution that you can loop through each item and get the description next to the term name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.