0

I have content type with a term reference field. Let's call it 'Color'. I want the content type to display markup depending on which term has been selected in the term reference field. For example, if 'Color' is Red, then the markup field will display 'Red is the color of fire'. If 'Color' is Purple, the markup field will display 'Purple is the color of royalty'.

Any ideas?

Thanks

1 Answer 1

1

You need to implement hook_preprocess_HOOK(&$variables), and add processing there, like this:

<?php

function MYTHEME_preprocess_node(&$variables) {
    // Check if field tid (term id) is filled up
    if (isset($variables->field_term_color[LANGUAGE_NONE][0]['tid'])) {
        $color = $variables->field_term_color[LANGUAGE_NONE][0]['tid'];

        $text = '';
        // You need to look up tid of each color
      switch ($color) {
        case 123:
            $text = 'Red is the color of fire.'
            break;
      }

      // Assign value to markup (text) node field
      $variables->field_markup_field[LANGUAGE_NONE][0]['value'] = $text;
    }
}

?>

You should place the right field names instead of the ones I use, of course.

2
  • In the end, I wound up creating a select list field since there were only a few options and populating it with Views Bulk Operations but your answer is the best possible one to my question. The code example makes it extremely useful. Thank you Alexei.
    – user320691
    Commented Apr 16, 2014 at 15:29
  • This is wonderful that you found an alternative solution, that does not involve coding. Commented Apr 16, 2014 at 15:32

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.