1

I am using a taxonomy to categorize content of type "lesson plan" by subject. Each lesson plan can belong to multiple subjects.

In a view where I display these lesson plans, I would like to have a field that lists all of the term's descriptions, rather than their names.

I followed these steps to pull in Taxonomy term: Term description instead of Content: Subject (where Subject is the name of the vocabulary) in the view.

However, in the Rewrite Results area, [description] only pulls in the first term's description, instead of all of them.

How do I get all of them?

6
  • Why do you need all of them in a single row? By adding a relationship to the taxonomy term your view should now have x rows, where x is the number of values for the field. Each of those rows has its own term description which gets printed separately...unless I've misunderstood something?
    – Clive
    Apr 28, 2017 at 20:44
  • Ok yeah I misunderstood, thought this was a single content page but of course it's a list. What format are you trying to display the content in? Is it a heading (content title maybe) followed by a list of term descriptions?
    – Clive
    Apr 28, 2017 at 20:47
  • Currently it's in a special box below the heading. The term names are 1, 2, 3, etc. (representing 1st, 2nd, 3rd grade) and the descriptions are 1st, 2nd, and 3rd, respectively. I want the comma separated list of terms to say: "1st, 2nd, 3rd" rather than "1, 2, 3" Apr 28, 2017 at 20:49
  • Aggregation is usually the way to sort that out but it can be a headache. If you're happy writing a custom module to solve it I can put some sample code for a (potentially) simpler solution in as an answer
    – Clive
    Apr 28, 2017 at 21:00
  • Thank you! My boss is somewhat (not deal-breaking my but still) averse to custom modules -- can you tell me more about aggregation? Apr 29, 2017 at 6:20

1 Answer 1

0

One quick way to solve it with code would be to add a custom field formatter:

function MYMODULE_field_formatter_info() {
  return [
    'taxonomy_term_csv_descriptions' => [
      'label' => t('Taxonomy term CSV descriptions'),
      'field types' => ['taxonomy_term_reference'],
    ],
  ];
}

function MYMODULE_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = [];

  if ($display['type'] == 'taxonomy_term_csv_descriptions') {
    $descriptions = [];

    foreach ($items as $item) {
      $descriptions[] = check_markup($item['taxonomy_term']->description, $item['taxonomy_term']->format);
    }

    $element[0] = ['#markup' => implode(', ', $descriptions)];
  }

  return $element;
}

That will give you a new formatter to use for taxonomy term reference fields in Views or the Manage Display pages for content types.

If you go with this solution you can remove the relationship to the Taxonomy Term from your View.

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.