0

I have a View that displays Nodes. Each of the nodes have a taxonomy term applied to it.

I would like this taxonomy term to be display as it's Term-ID number. How can I do this?


Steps I have tried

  1. In relationships, I have added "Content: Taxonomy terms on node". This is the only option for taxonomy terms in a node View. But it doesn't have options for accessing the term ID.

  2. In fields, you can add a field to display the term on a Node. I have checked "Rewrite the results". There is a token called Raw, but it only displays "Raw Text" as opposed to a number.

2

Here are few things you need.

  1. In relationships, add Content: Taxonomy terms on node.
  2. In Filter Criteria(I think this is the one you are missing), add Content: has taxonomy term and check the "is not empty(NOT NULL)".
  3. In Fields: Add Taxonomy term: Term ID

PS: in step 2, you may pick other operator based on what you need.

  • This works except now each node appears 4 times. I have tried enabling the distinct option under "Query Settings". I have also turned on "Reduce duplicates" under "Filter Criteria Content: has taxonomy term". Thanks for any help you can offer. – big_smile Mar 20 '15 at 14:44
  • In "Relationships", you need to specify the taxonomy vocabulary. If you do this, and set "Query Settings" to distinct (no need for pure distinct), it will work! – big_smile Mar 21 '15 at 12:11
  • good to hear u got fixed :) – CocoSkin Mar 22 '15 at 23:07
0

In the majority of Views you indeed can use relationships (see CocoSkin's answer), but in some Views, like revisions, it doesn't work (you get only the last revision of the field). The best approach in such a case is to create a new field formatter in your private module:

/**
 * Implements hook_field_formatter_info().
 */

function yourmodule_field_formatter_info() {
  return array(
    'yourmodule_termid' => array(
      'label' => t('Show TermID'),
      'field types' => array('taxonomy_term_reference'),
    )
  );
}

/**
 * Implements hook_field_formatter_view().
 */

function yourmodule_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  if (!in_array($entity_type, array('node', 'field_collection_item'))) {
    return $element;
  }

  switch ($display['type']) {

    case 'yourmodule_termid':

      foreach ($items as $delta => $item) {
        foreach ($item as $key => $val) {
          if ($key == 'tid') {
            $element[] = array(
              '#markup' => $val,
            );
          }
        }
      }

      break;
  }

  return $element;
}

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.