Achieve Taxonomy Image function in Drupal 7 with Imagefield
Now when Taxonomy is an entity, and an entity can have fields; Taxonomy Image functionality can be achieved by adding a Image field to a vocabulary.
How to do this
- Enable Image module and its required modules.
- Go to the Taxonomy settings page (Administration » Structure » Taxonomy) and choose edit for the vocabulary that you would like to associate images and terms – and then go to Manage fields in the tabs menu.
- Now add a image field and make the necessary settings for that field.
- Now you have a image field to add images, when you add or edit terms that belong to the vocabulary that has the image field.
Display taxonomy images inside nodes
In the, not so crazy, scenario that you want to display the taxonomy image you just created inside a node, you can do this in Drupal 7 with the use of some extra modules.
First, you will need Entity API and EntityReference installed and enabled. Optionally, you may also want to have Entity View Mode so you can create a custom view mode for displaying the taxonomy term with just the image.
Then, follow the steps below:
- Go to the taxonomy admin page (Administration » Structure » Taxonomy), select edit on the vocabulary you wish to use. Then go to "Manage Display" to activate and edit a custom display to show the image.
- Go to your content type, add a field of type "Entity Reference", and on the "Field Settings" page set the target type to "Taxonomy Term" and select the vocabulary you want to use in "Target Bundles". Finish editing and save.
- Go to "Manage Display" for the content type this time. For the Entity Reference field select the Format column to be "Rendered Entity" and after clicking the gear icon, select the view mode you just edited.
- Optional: You may wish to use a custom taxonomy-term.tpl.php in order to render the taxonomy image without the term tag. See the code sample at bottom.
Display taxonomy images in Views
In Views you just need to add the Entity Reference field to the "Fields" section, select the formatter to be "Rendered Entity" as above, and use the same view mode, or another if you prefer. Using this method there is no need to add a relationship.
Note
It should be noted that you are essentially using Entity Reference in place of the normal core Term Reference in order to get more flexible display options. If you have to use the core Taxonomy Reference you may have to write a new field formatter, or you can use the Taxonomy Image module for Drupal 7 (still only in Git currently).
Code sample for taxonomy-term.tpl.php
If you are rendering the taxonomy term entity via entity reference, and have a custom view mode called taxonomy_term_image and a field on the taxonomy term called field_term_image, the following could be used to only display the image when you are referencing it.
<?php if($view_mode != 'taxonomy_term_image'): ?>
<div id="taxonomy-term-<?php print $term->tid; ?>" class="<?php print $classes; ?>">
<?php if (!$page): ?>
<h2><a href="<?php print $term_url; ?>"><?php print $term_name; ?></a></h2>
<?php endif; ?>
<div class="content">
<?php print render($content); ?>
</div>
</div>
<?php else: ?>
<?php if(!empty($content['field_term_image'])): ?>
<h2>
<a href="<?php print $term_url; ?>">
<?php print render($content['field_term_image'])?></a>
</h2>
<?php endif; ?>
<?php endif; ?>
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion