Except by creating term views, browsing single taxonomy terms or by querying the term_fields_term table in the database, how can I expose data stored in the Term Fields module to end users? Is it required/possible to use computed field to display Term Fields data?

I want to use Term Fields to store a term's international code number. Eg. use Term Fields to store a Zip code, while term name and term description are storing place name. (silly example, since there are specialized modules for Zip codes). It would by much easier to maintain a vocabulary than by creating nodes of every vocabulary term.

CommentFileSizeAuthor
#4 term_fields.patch4.4 KBb-prod

Comments

milo2man’s picture

I would also like to know this! I have created a custom taxomony_terms view using Views. I have used the following PHP snippet to display the term description in the header of each page and would like to add the custom term fields to the header as well.

I created a new term_field called "definition". Just trying to reference this with echo $current->definition doesn't work. How do I add this to the header?

<?php $current = taxonomy_get_term(arg(2)); ?>
<?php if ($current): ?>
    <div class="taxonomy-description">
        <?php echo $current->description; ?>
    </div>
<?php endif; ?>

If this method works, it would be possible to use the term fields with a nodes view, as well, via the page header.

rhache’s picture

+1 for this.

I'd like to know how to fetch the term's field data on a node, without resorting to Views. Without going into details, I want to use term field on the node that would not work with views. I can fully illustrate a scenario if that would make a difference.

Some kind of load function, like node_load, would be great.

Rene

summit’s picture

+1 also in need of showing term fields!
Greetings, Martijn

b-prod’s picture

Version: 6.x-1.10 » 6.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new4.4 KB

This patch add fields information in each terms present in $node->taxonomy. The fields are accessible in $node->taxonomy[XYZ]['fields'] where XYZ is the term tid.

It also add tokens for node, even for presave operation, so tokens may be used for pathauto replacements. It is supposed to handle tags vocabularies.

Before applying this patch, I want that some community users test it, especially with tokens and modules which use token replacements, like pathauto.

Thanks.

kthull’s picture

Patch works beautifully with auto node titles. Thanks!

ryanilg’s picture

You can also use any of the following functions included in the module. I just found these by looking at term_fields.module.

/**
* Retrieve a single field value given a term ID and field ID.
*/
function term_fields_get_field($tid, $fid) {
return db_result(db_query("SELECT %s FROM {term_fields_term} WHERE tid = %d", $fid, $tid));
}

/**
* Retrieve an array of fields from a given term, keyed by field ID.
*/
function term_fields_get_fields($term) {
if ($fids = term_fields_get_fids($term->vid)) {
$field_list = implode(',', $fids);
$sql = 'SELECT ' . $field_list . ' FROM {term_fields_term} WHERE tid = %d';
$result = db_query($sql, $term->tid);

return db_fetch_array($result);
}

return NULL;
}

/**
* Retrieve a term object like taxonomy_get_term(), but with fields.
*/
function term_fields_get_term($tid) {
$term = taxonomy_get_term($tid);
$fields = term_fields_get_fields($term);
$term->fields = $fields;

return $term;
}

b-prod’s picture

Status: Needs review » Fixed

Ported in the dev version.

Status: Fixed » Closed (fixed)

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

FamousNerd’s picture

Ive created the page-taxonomy-term.tpl.php file and I'd like to pull the value of the field into the taxonomy page. When I try to the following code:

$term = term_fields_get_term(arg(2)); (where arg(2) is the integer id of my taxonomy term)

the error log shows PHP Fatal error: Call to undefined function term_fields_get_term()

Are these functions available from within template files?

Many thanks
Erik

b-prod’s picture

What version of the module do you use? The 2.x does no more use such function, since the data are attached directly to the node->taxonomy array.

This issue only concern 1.x version.

FamousNerd’s picture

Good point, I just checked and I'm on the following version: "6.x-2.0-alpha2"

Are you able to suggest to me the php syntax to retrieve values in the newer API version?

I should note, the page that I would like to show this on is a page display for a view. It's a list page and below that table which is the listing I want to show the taxonomy term's Field Value as "body text"

I'm not sure what this does for "$node", does that make it the taxonomy term's node? because $node->taxonomy seems empty.
Thanks
Erik

b-prod’s picture

@FamousNerd: the $node->taxonomy is populated in node template files when viewing a node. So it does not apply to Views module displays which use "fields display". For that you have specific Views handlers.

I am not sure of what happen with node display, since I remember Views set node view modes ($a3, $a4) to 0. Moreover, the taxonomy term module uses hook_links() to alter the taxonomy array, so if you choose to not display the link, the taxonomy fields will be empty.

I hope it will help you to debug your issue. If you need some more help, could you please open a new issue against the 2.x version and describing your problem more accurately?