0

when I choose a taxonomy term with a parent, it shows -termName instead of termName, to indicate it is a sub-category. I checked the database, it only stores the termName, which means that somewhere we can rewrite the output to show just termName.

How can I achieve this?

1
  • 2
    It is most probably the theme layer which rewrites the output. I'd recommend you to check your theme's files.
    – Elin Y.
    Commented Sep 8, 2013 at 18:55

1 Answer 1

2

In Drupal 6, the function that outputs the list of taxonomy terms is _taxonomy_term_select(), which contains the following code.

  if ($tree) {
    foreach ($tree as $term) {
      if (!in_array($term->tid, $exclude)) {
        $choice = new stdClass();
        $choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
        $options[] = $choice;
      }
    }
  }

  return array(
    '#type' => 'select',
    '#title' => $title,
    '#default_value' => $value,
    '#options' => $options,
    '#description' => $description,
    '#multiple' => $multiple,
    '#size' => $multiple ? min(9, count($options)) : 0,
    '#weight' => -15,
    '#theme' => 'taxonomy_term_select',
  );

Since it uses theme_taxonomy_term_select(), overriding that theme function is possible to remove those hyphens added at the beginning of the term name.

In Drupal 7, that code is contained in taxonomy_allowed_values().

  $options = array();
  foreach ($field['settings']['allowed_values'] as $tree) {
    if ($vocabulary = taxonomy_vocabulary_machine_name_load($tree['vocabulary'])) {
      if ($terms = taxonomy_get_tree($vocabulary->vid, $tree['parent'])) {
        foreach ($terms as $term) {
          $options[$term->tid] = str_repeat('-', $term->depth) . $term->name;
        }
      }
    }
  }

Unfortunately, the function calling it (options_field_widget_form()) uses a theme function used for every form element (including the ones not associated with taxonomy terms), for example theme_select().

  switch ($type) {
    case 'select':
      $element += array(
        '#type' => 'select',
        '#default_value' => $default_value,

        // Do not display a 'multiple' select box if there is only one option.
        '#multiple' => $multiple && count($options) > 1,
        '#options' => $options,
      );
      break;

    // …

  }   

Fortunately, options_field_widget_form() uses some custom properties that allows to check if the select form element is being output for an entity. In fact, it uses the following code.

$entity_type = $element['#entity_type'];
$entity = $element['#entity'];

This is true also in the case the used form element is checkboxes, radios, or checkbox.
If $variables['element']['#entity_type'] passed to the theme function for the select form element is set, and it is equal to the entity type for the taxonomy terms, then the theme function can assume it is outputting the select element for a taxonomy term, and remove the hyphens at the beginning of the term name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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