Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
Deciphered CreditAttribution: Deciphered commentedYes, 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.
Comment #2
NexusStar CreditAttribution: NexusStar commentedThanks 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.
Comment #3
Deciphered CreditAttribution: Deciphered commentedDoes 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):
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.
Comment #4
NexusStar CreditAttribution: NexusStar commentedThanks a lot Deciphered here what I come up from your guidance
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
Comment #5
Deciphered CreditAttribution: Deciphered commentedHi 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.
Comment #7
antpre CreditAttribution: antpre commentedThanks 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
Comment #8
antpre CreditAttribution: antpre commentedTo 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.
Comment #9
Deciphered CreditAttribution: Deciphered commentedAntpre,
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.
Comment #10
Summit CreditAttribution: Summit commentedHi, Antpre, Deciphered. Would love to see the code for this!
Greetings, Martijn
Comment #11
Deciphered CreditAttribution: Deciphered commentedIt'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:
Here's the CTools exportable version:
Comment #12
antpre CreditAttribution: antpre commentedMany 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.
Comment #13
Deciphered CreditAttribution: Deciphered commented@atrpre,
I've improved the preview function in the latest commit, dev build should be available shortly, should now work with any field type.
Comment #14
yogurtu CreditAttribution: yogurtu commentedHello 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.
Comment #15
Deciphered CreditAttribution: Deciphered commentedHi 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:
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.Comment #16
yogurtu CreditAttribution: yogurtu commentedHi!
$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!)
Comment #18
mistymountain CreditAttribution: mistymountain commentedWhere do I put all this code?
Comment #19
davewilly CreditAttribution: davewilly commentedThis may be useful to someone.
Render a taxonomy image field with an image style and link:
Comment #20
pumablues CreditAttribution: pumablues as a volunteer commentedComment #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.
Comment #21
Kris77 CreditAttribution: Kris77 commentedHi 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!