3

I have a nodes, and a few vocabularies to choose terms from. I would like to show images of terms from specific vocabulary in nodes (If node is associated with term from specific vocabulary, it would show image from that terms field). What would be the best (most "Drupal") approach to this? :)

14

One way to do it -- if you don't want to create/mess with the theme template files for your content type, would be to display the corresponding tag image via a view.

Here are the steps to doing that:

  1. I'm assuming you already have your vocabulary set up properly and you have the image field added to it (can be done at admin/structure/taxonomy/[YOUR VOCAB ID]/fields) and some terms have been populated with their corresponding images.
  2. Create a new view with a block display, and set it to apply only to the relevant content types enter image description here
  3. Add a term relationship to your view, so you can access the vocab/term fields, and not just the node fields!! enter image description here enter image description here
  4. Once you've done that, go into the fields you want to pull for each node. I'd remove the 'Title' field that's added by default and only add the field that represents the image associated with your term. It should be somewhere under the 'Taxonomy term:....' section of the field list.
  5. Make sure that you set it to use the relationship you created above enter image description here
  6. Finally, since we'll want this block to take its cue of which image to display based on which node it's on, let's add a contextual filter. Under the 'Advanced' section --> 'Contextual filters', add the 'Content: Nid' one and make sure you set it up to take its NID from the page it appears on if a filter isn't present in the URL enter image description here
  7. All done! Go to one of the relevant pages and test. Of course, you'll have to enable the block via contexts or the block admin page (admin/structure/block) first, since it's not placed anywhere by default. Display the block on the second sidebar, in the actual content section -- anywhere you want.

Let me know if this helps!!

================ EDIT ===================== Solution 2 (upon request)

To do this through your template file, go ahead and copy your regular node.tlp.php file and rename it node--[YOUR CONTENT TYPE MACHINE NAME].tpl.php

Then, right under or above the content, insert the following code:

$term=taxonomy_term_load($node->field_thats_term_reference['und'][0]['tid']);   
$image_field=field_view_field('taxonomy_term', $term,'field_main_image');
print render($image_field);

Just substitute 'field_thats_term_reference' with the name of your term reference field, and substitute 'field_main_image' with your image field name.

That'll do the print out. You can place this anywhere you want. If you want to customize how the print out actually looks, just do a dpm($image_field) once you've set that variable (must have the devel module installed), and you can see all the parts of that field and how to get to them if you want to print them out separately as opposed to the default way render() prints it all out. I think that part is beyond the scope of this question, but it should get you started!

  • Thanks! That works, but I'm also interested in achieving this by creating/manipulating template files. Could you add something about it to your answer? :) That would be really epic answer ^^ . – Łukasz Zaroda Jul 16 '12 at 18:37
  • 1
    Alright, you've talked me into it. I've edited the answer and included that.... – Boriana Ditcheva Jul 16 '12 at 18:57
  • 2
    I think there should be if($term=taxonomy_term_load($node->field_thats_term_reference['und'][0]['tid'])) { /* the rest */ } otherwise I'm getting error :) (info for readers) . – Łukasz Zaroda Jul 17 '12 at 23:40
  • Yep, you're totally right. If this term is not required for the content type... Very nice catch! – Boriana Ditcheva Jul 18 '12 at 4:13

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.