I would like to be able to display extra fields added to vocabulary is it possible to be done with custom formatter module and how?
To be more precise I create new vocabulary in which I add extra Image field and would like to be able to display this extra field at node where taxonomy term are referenced.
All help will be greatly appreciated

Comments

Deciphered’s picture

Status: Active » Fixed

Yes, it is.

You need to make a Custom Formatter for the Taxonomy Term reference field (taxonomy_term_reference) and output the relevant data that is attached. Theoretically it could be done with a HTML+Token based formatter, but I don't know where the state of the Token module is anymore and have given up on following the issue.

It would be extremely easy to do with a PHP formatter, but you will need to know PHP.

Cheers,
Deciphered.

NexusStar’s picture

Thanks for your answer and help I try to make custom formatter as you suggested but without success.

As I'm not good with PHP I try to use HTML + Tokens with following code

<a href="[node:field-term:url]"><img src="[node:field-term:field_image]" alt="[node:field-term:i18n-name]" title="[node:field-term:i18n-name]" /></a>

as expected the tokens
[node:field-term:url]
[node:field-term:i18n-name]

result in what ones expects
but
[node:field-term:field_image]
does not get replaced with image field value

I took this tokens from available tokens list from module UI.

If you could show to me how this could be achieved with PHP or point to me where to look on some similar code.

Deciphered’s picture

Status: Fixed » Active

Does the term being referenced contain an image?

The PHP version should be relatively easy something like the following (off the top of my head and clearly not usable):

$tid = [the term id from the node object];
$term = taxonomy_term_load($tid);

$image = theme('image', array(
  'path' => [the path to the file from the term object],
  'alt' => [the relevant item from the node object],
  'title' => [the relevant item from the node object],
));
return l($image, [the term URL], array('html' => TRUE));

You'll need the Devel module installed and enabled to see the data passed to the relevant objects to fill in the blanks.

Hope this helps.

Cheers,
Deciphered.

NexusStar’s picture

Thanks a lot Deciphered here what I come up from your guidance

$tid = $items[0]['tid'];
$term = taxonomy_term_load($tid);
//it turn out that taxonomy_term_load gets empty array for my custom field 
//that's why  after some poking around Drupal API i come up with the following
$term_page =  taxonomy_term_view($term, $view_mode = 'full');
$term_url = taxonomy_term_uri($term);
$image_url=$term_page['#term']->field_client_logo; //this is my custom field name

$image = theme('image', array(
  'path' => $image_url['und'][0]['uri'], 
  'alt' =>  $term->name,
  'title' => $object->title,
));
return l($image, $term_url['path'] , array('html' => TRUE));

And it works great and does just what I want to show image from custom vocabulary field instead of term name on node page.
As I am not programmer if someone spot some error or could show some improvement on my code it will be greatly appreciated.

And again BIG Thanks Deciphered if I meet you someday you've got a beer at least from me

Deciphered’s picture

Status: Active » Fixed

Hi NexusStar,

Glad I could be of some assistance and that everything is all working for you, and I'll be happy to take you up on the Beer one day :)

Cheers,
Deciphered.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

antpre’s picture

Thanks to both of you.

It works perfectly... I had been searching for a simple way of handeling taxonomy image in a simple way for a long time.

Is it possible to improve the code so that it can handle multiple value term field ???

When I select 2 terms, it diplays only one icon.

Thanks

antpre’s picture

Status: Closed (fixed) » Active

To be more precise about my query. I read and saw that custom formatters module handle multiple values field.
But in the case of the code above. It doesn't display the multiple values of the taxonomy term targeted. Only the first value is displayed.
Any help will be apreciated.

Deciphered’s picture

Status: Active » Fixed

Antpre,

All you need to do is a foreach loop over the $variables['#items'] array around that logic and it will do all terms.

If you still need more help I'll paste in the code tonight (only if you indicate you need more help).

Cheers,
Deciphered.

Summit’s picture

Hi, Antpre, Deciphered. Would love to see the code for this!
Greetings, Martijn

Deciphered’s picture

It's pretty straight forward, everything I've mentioned above is what I used for the following example. The example specifically targets an image field, but the code could be modified however necessary:

// Set the name of the field you wish to display from the reference term.
$field_name = 'field_image';

$output = '';
foreach ($variables['#items'] as $item) {
  $term = taxonomy_term_load($item['tid']);
  $output .= theme('image', array('path' => $term->{$field_name}[LANGUAGE_NONE][0]['uri']));
}
return $output;

Here's the CTools exportable version:


$formatter = new stdClass();
$formatter->disabled = FALSE; /* Edit this to true to make a default formatter disabled initially */
$formatter->api_version = 2;
$formatter->name = 'taxonomy_reference';
$formatter->label = 'Taxonomy reference';
$formatter->description = '';
$formatter->mode = 'php';
$formatter->field_types = 'taxonomy_term_reference';
$formatter->code = '// Set the name of the field you wish to display from the reference term.
$field_name = \'field_image\';

$output = \'\';
foreach ($variables[\'#items\'] as $item) {
  $term = taxonomy_term_load($item[\'tid\']);
  $output .= theme(\'image\', array(\'path\' => $term->{$field_name}[LANGUAGE_NONE][0][\'uri\']));
}
return $output;';
$formatter->fapi = '';
antpre’s picture

Many thanks Deciphered,

For your module and this code.
I am sure this will interest a lot of poeple how want to acheive taxonomy icon / image and are not confortable with php.

I v noticed that with the version 7.2 of the module the preview function is not working anymore for taxonomy term reference (works ok with text, image...). it was working with the previous release for me. I don't know if its my configuration or if its a bug. This is why I am not posting it as a bug.

All the best.

Deciphered’s picture

@atrpre,

I've improved the preview function in the latest commit, dev build should be available shortly, should now work with any field type.

yogurtu’s picture

Hello Deciphered, again thank you for the module!

I'm using a version of NexusStar, for a node reference field in a multilang site, here's the code:

global $language;
$nid = $items[0]['nid']; // to get the node referenced nid
$publicacion = node_load($nid); // field_publicacion is the node reference I'm usign

$node_page = node_view($publicacion, $view_mode = 'full');
$node_page_title = node_page_title($publicacion);
$node_url = node_uri($publicacion);

// here's the code for multilang
switch ($language->language) {
case "es":
return l("Lea el artículo completo en la edición ".$node_page_title, $node_url['path'] , array('html' => TRUE));
break;
case "en":
return l("Read the whole article in ".$node_page_title, $node_url['path'] , array('html' => TRUE));
break;
}
------

Unfortunately , I couldn't get the description and an image field (field_portada) from the referenced node using:

$image_url=$node_page['#nid']->field_portada; //this is my custom field name
$image = theme('image', array(
'path' => $image_url['und'][0]['uri'],
'title' => $object->title,
));
return l($image, $node_url['path'] , array('html' => TRUE));

I must be doing wrong something in this node reference version... I would appreciate some help.

Deciphered’s picture

Hi Yogurtu,

You should grab the additional data you need from the raw node ($publication) not from the rendered node ($node_page). Stick dpm($publication); in your formatter and you should be able to see all the available data to work with. I would also go so far as saying that there's actually no need to get a rendered version of the node at all and that doing so is an unnecessary performance hit.

You should be able to get your publication fields as such:

$node_page_title = $publication->title;
$node_url = url("node/{$publication->nid}");

However, it's possible that you're rendering the node for the purposes of translation...

Also, another note, as you are returning inside of the switch/case there is no need to break;, as you've already returned and the break is never being hit.

yogurtu’s picture

Hi!

$node_url = url("node/{$publication->nid}");
gives me a wrong url, so I kept
$node_url = node_uri($publicacion);
until I get it rigth.

Performance is really important, thank you Deciphered!
(also, for the nice&helpfull video on youtube!)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

mistymountain’s picture

Where do I put all this code?

davewilly’s picture

This may be useful to someone.
Render a taxonomy image field with an image style and link:

<?php
// get term id from available $items variable
$tid = $items[0]['tid'];
// load term object from id
$term = taxonomy_term_load($tid);

// get specified field from term object
$image = field_get_items('taxonomy_term', $term, 'field_brand_logo');
// render image with a style and link
$out = render(field_view_value('taxonomy_term', $term, 'field_brand_logo', $image[0], 
		array(
			'type' => 'image',
			'settings' => 
                          array(
                            'image_style' => 'brand_logo', // name of image style
                            'image_link' => 'content' // will link to term url
                          )
		)
	)
);
return $out;
?>
pumablues’s picture

Issue summary: View changes

Comment #4 above in conjunction with the Custom Formatters module solved my problem perfectly. I was trying to do exactly what NexusStar was trying to do, and getting really frustrated. Thanks to both NexusStar and Deciphered, for the solution and the module both! If anyone has questions on how to use the module to acheive this, PM and I'll do my best to get back to you and explain how I used this solution on my site.

Kris77’s picture

Hi guys.
I create new vocabulary in which I add extra LINK field(LINK_CAMPO) and would like to be able to display this extra field at node where taxonomy term are referenced.
I have installed custon formatter module, enable it. Now in my content type there is a field(CAMPO) of Entity Reference type.

In FORMATTERS configuration create a new type with this settings:
BASIC INFORMATION: Name:THAT FIED and format: PHP
FIELD TYPE(s): Entity Reference

Now, what kind of code must be insert into EditAREA?

I use Drupal 7.39 with Custom Formatters 7.x-2.4

Thank a lot!