I know that if one knows how to write php code, then you can put a snippet in the header of your taxonomy view to display the related taxonomy description. I found this snippet of code for 6.x-3.0, but it doesn't work for 7.x.
<?php
$tid = arg(2);
$term = taxonomy_get_term($tid);
if (module_exists('i18ntaxonomy') && i18ntaxonomy_vocabulary($term->vid) == I18N_TAXONOMY_LOCALIZE) {
$description = i18nstrings("taxonomy:term:$term->tid:description", $term->description);
}
else {
$description = $term->description;
}
print $description;
?>
Can anyone help me adapt this for Drupal 7?
Or does anyone know of a different way to display the taxonomy's description at the top the that particular term's page?
Comments
Comment #1
traceelements commentedUpon further inspection of Drupal 7, I see that I can add fields to each taxonomy term, such as a image field, as well as a description field. How do I get all of those fields to display in a custom taxonomy view?
Comment #2
dawehnerDid you find out already which part of the code does not work?
* Is $term loaded?
* Is $term->description not set?
Comment #3
traceelements commentedI get this error:
Fatal error: Call to undefined function taxonomy_get_term() in /xxx/modules/php/php.module(75) : eval()'d code on line 3
Comment #4
dawehnerIn d7 you have to use taxonomy_term_load($tid)
Comment #5
traceelements commentedThank you, dereine. I'm not getting the error anymore, but the description still won't show up in my view. Any ideas?
Comment #6
traceelements commentedComment #7
Andrey Zakharov commentedworks for me
Comment #8
dawehnerOkay try this code.
Comment #9
Damani commentedi am trying to set up a block to display related content using drupal 7. So depending on the node you are viewing it should display similar content related to that node by taxonomy, similar to how youtube has it when viewing a video.
currently i tried to set the default argument by inserting exactly this code:
$node=node_load(arg(1));
if($node){
foreach($node->taxonomy as $term){$terms[]=$term->tid;}
return implode('+',$terms);
}else {return;}
but alas the block does not show up. instead i get the following error:
Notice: Undefined property: stdClass::$taxonomy in eval() (line 3 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Warning: Invalid argument supplied for foreach() in eval() (line 3 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Notice: Undefined variable: terms in eval() (line 4 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Warning: implode() [function.implode]: Invalid arguments passed in eval() (line 4 of C:\xampp\htdocs\cxcplus2\sites\all\modules\views\plugins\views_plugin_argument_default_php.inc(51) : eval()'d code).
Comment #10
dagmarDamani has already posted other issue for his problem #1058674: Only works with first taxonomy term views3 D7. Reverting status of this issue.
Comment #11
Damani commentedThe title change was by mistake. Sorry my bad.
Comment #13
borazslo commentedthx!
Comment #14
jon nunan commentedI updated my dev version after the weekend and now my term description fields generate 'notices' and are output as plain text. Anyone else getting this?
I added a textarea field to my taxonomy term in which I copied the description into. I then removed the description field from my view and it is now displaying fine. So I think the problem is just with 'term description'.
The notices generated when displaying the 'description' field is:
The taxonomy is tied to the content through a Term Reference field called 'category' and it was working fine in earlier builds this month.
Comment #15
elgandoz commentedsubs
Comment #16
rt_davies commentedNow when you visit the Taxonomy Term view, your new fields view will be included at the top.
Comment #17
rocbrook commentedrt_davies' solution worked like a charm. I didn't need to create a separate description field as it showed OK for me.
However, this seems like it should work right out of the box, the fields show fine on the default term display so why don't they show in the view override?!
Thanks rt_davies!
Comment #18
mrP commented#7 works like a charm as well for a straight term description implementation.
@Andrey Zakharov - thank you kindly
Comment #19
jaypan#16 worked for me, but this point was a little unclear:
The contextual filter needs to be on the term ID.
Comment #20
lolandese commented#16 works now (D7.12) without creating a new text-area field. You can skip the first section. Thanks.
Comment #21
Azamat commentedThank you !
Comment #22
sahuni commentedBig Thanks, it works also for me now.
Just a problem with multilingual website
I use the default term description, I've translated it.
taxonomy views is filtered by language, but my description stay in my default language.
I tried in new view to add a filter by language, the only effect is that description disappears.
Any idea?
Comment #23
stephane bouillet commentedHie,
#16 also works for me (D7.14): Thank you very much !! :)
Comment #24
p55mac commented#16 Also worked for me.
But I had trouble with
as well. It came up saying
Whenever I tried to add a Taxonomy term: Term ID contextual filter. In the end I just pressed cancel and it left the filter in place... but it worked regardless.
Also, I could use the the existing/default field named "Description".
Comment #25
trupal commentedworks great on taxonomy view but how to display it in catalog view?
Comment #26
jwilson3UPDATE: see comment #28 below for the best D7 solution.
Here is an improved version of the solution in #7 that will respect the *text format* of the term description field:Comment #27
gratefulsk commentedAdding this php code to the header worked for me
Comment #28
dennisg commented#26 didn't work for me on Drupal 7.
But this one worked:
i18n_taxonomy_vocabulary_mode($term->vid) == 1 means that you have selected Localize translation mode on edit vocabulary page.
Comment #29
jwilson3Good call on #28, i didn't test the i18n integration in #26. Thanks.
Comment #30
betarobot commentedThanks @celticremark, #27 worked just lovely for me!
Comment #31
bendev commented#28
thanks !
Comment #32
danny englanderThis was a trivial matter of adding a relationship "
Add a relationship "Content: Taxonomy terms on node" and then adding in the Replacement pattern forTaxonomy term: Term descriptionfield into a global text header in the view ([description_field]).Comment #33
stoan commented@highrockmedia may you elaborate more, I'm try your solution, I'm don't understand what I'm suppose to do from here "then adding in the Replacement pattern for Taxonomy term: Term description field into a global text header in the view ([description_field])"
I have added the relationship on the taxonomy view. Thanks
Thanks
Siya
Comment #34
kaynen commented@stoan I believe highrockmedia means that you should add a "taxonomy terms: term description" field to the view, exclude it from display, and then add a "global: text area" to the header with the replacement pattern [description]. Let me know if that helps!
Comment #35
mrpauldriver commented@highrockmedia I am not sure that it is quite so easy if your view does not contain fields - when displaying rendered entities for example.
I don't code, but had a go at #26 #27 & #28. I just ended up with broken php showing in my header - not sure what I was doing wrong.
The post at #16 contains a good explanation of the issues and got me sorted out.
It does seem odd however, that the default taxonomy field has a bug that prevents its' display in a taxonomy view??
Comment #36
danny englander#33 and #35, it's a long time ago but I believe I added a description as a new field within my vocabulary and then that was available with views and all the goodness that comes with it.
Comment #37
shamerlinck commentedThis was a valid solution for what I was after. Just wanted to give a referral where it's due.
rt_davies' #16 worked here.
Similarly, as mentioned elsewhere on the thread, #6 isn't spelled out all the way. If you need it a little more descriptive, I used:
Contextual Filter:
Taxonomy term: Term ID
On configuration of the filter:
Clicked the 'Set Default Value' button,
and set it to 'Taxonomy term ID from URL'
and checked 'Load default filter from term page'
That did the trick for me.
Comment #38
firesidelibrarian commentedI have been searching for a way to do this for hours, and #16 + #19 worked like a charm for me. Thanks so much!
Comment #39
ProYoga.NL commentedTried #16, #19 and #37, but eventually gave up after many failed attempts. Remaining problem with my configuration, view would only take the first taxonomy term description from all the content nodes that shared the same vocabulary or create multiple duplicates, instead of selecting only the description of the selected taxonomy term.
The following links were helpful to understand the required steps (and possible shortcuts), but didn't provide the final solution:
http://pixelheadstudio.com/blog/solved-drupal-7-missing-taxonomy-term-de...
http://sergiy.kyrylkov.name/2012/02/29/showing-taxonomy-description-in-t...
In my case it was #28 that provided the fix I was looking for. Plus enabling Query Settings > Distinct. Thanks for sharing!
Comment #40
Anonymous (not verified) commentedThank you, rt_davies! This was EXACTLY what I needed for a project.
Comment #41
dries arnoldsFollowing the method in #32 I couldn't get rid of duplicates with distinct only and both distinct and pure distinct.
Then I tried #16 which works very well. Only difference is that I used a block display, since I didn't need the term fields to be accessible by users/search engines apart from the taxonomy/term listing.
Comment #42
aamato commentedI can't believe, I still have the same problem. And this bug wasn't solved yet. I can only find workarounds, no definitive solution...
Comment #43
bill.zero commentedIn case you haven't found other threads about PHP code in Views headers in D7, you need two more things:
1) Enable PHP Filter module;
2) Change the Text Format of the Global:Text Area to "PHP Code".
#27 worked for me.
Comment #44
selfirian commented#27 worked for me as well
Comment #45
designSHIFT commentedHere is an export of the view that worked for me:
$view = new view();
$view->name = 'taxonomy_header_desc';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'taxonomy_term_data';
$view->human_name = 'Taxonomy Header & Desc';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '1';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_taxonomy'] = TRUE;
/* Field: Taxonomy term: Term description */
$handler->display->display_options['fields']['description']['id'] = 'description';
$handler->display->display_options['fields']['description']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['description']['field'] = 'description';
$handler->display->display_options['fields']['description']['label'] = '';
$handler->display->display_options['fields']['description']['element_label_colon'] = FALSE;
/* Contextual filter: Taxonomy term: Term ID */
$handler->display->display_options['arguments']['tid']['id'] = 'tid';
$handler->display->display_options['arguments']['tid']['table'] = 'taxonomy_term_data';
$handler->display->display_options['arguments']['tid']['field'] = 'tid';
$handler->display->display_options['arguments']['tid']['default_action'] = 'default';
$handler->display->display_options['arguments']['tid']['default_argument_type'] = 'taxonomy_tid';
$handler->display->display_options['arguments']['tid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['tid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['tid']['summary_options']['items_per_page'] = '25';
/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block_1');
Comment #46
vorbian commentedThank you rt_davies #16. Total genius. Not only did this help me for my current project to put my taxonomy description and image in the taxonomy view header, but it opened my eyes to a whole new way to add headers (which contain other views) to a current view. Awesomesauce!
Comment #47
giorgoskif #1957266: Rendered entity in views header accepting arguments from URL gets committed things will be simpler
until then the simpler solution I found is
1. create view with dump from #45 comment (adjust to your liking)
2. go your ORIGINAL taxonomy view and add Header: "Global: View area"
choosing the view from 1. and check "Inherit contextual filters"
Comment #48
Emiliano commentedThank you, rt_davies #16! It works perfectly!
Comment #49
buddym commentedJust adding a little note here for those that find this thread when searching for a fix on how to get your Taxonomy Terms to display the Description field (and other adhoc fields that can optionally/subsequently added to a Taxonomy vocabulary, i.e., Manage Fields/Display). The fix is to simply disable the "Taxonomy term" view and let Drupal handle the display like any other node.
Comment #50
rwilson0429 commentedAn old thread but a note to myself and others who come here looking for a solution to display a term's description (or any other term field) on the Taxonomy Term View page that ships w/Views. I'm not sure why in d7 others would find it necessary to write php code to display "taxonomy term description on the taxonomy views page". Neither do I agree with a solution that would require me to "disable the Taxonomy Term view and let Drupal handle the display like any other node".
The solution that seems to work for my installations are:
Comment #51
bramvandenbulcke commentedI know this is a Drupal 7 thread but anyone looking for a solution for Drupal 8: solution #16 also works well on Drupal 8!
Comment #52
shane birley commentedAnother method that people could try is by creating a block that displays the fields you want. After creating the block, you would display it at the top of the taxonomy terms page.
Comment #53
nelslynn commentedNothing is working for me here and I believe it's because my taxonomy term page is 2 deep. For example, I'm trying to display the description on a term page structure like this: catalog/main-term/sub-term (catalog/%/%). I want the description of the last term in the URL to display before the nodes teasers associated with the sub-term.
I hope this makes sense. I've searched all over for a solution and come up empty. If anyone has some incite, I'm all ears. Thx