accessing "cck taxonomy" field value in php code for "computed" field value

gjfreakout - September 5, 2007 - 01:58
Project:CCK Taxonomy Fields
Version:5.x-1.1
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

I am newbie to Drupal as well as PHP development,

I created a new content form and in it i am using few "cck taxonomy" fields. say filed name is "field_state" and taxonomy name is "state"
I also created a computed field and want to access tid and/or name of the term selected in the above field_state field.

I tried many variables including field_state[0]['tid'] but could not access the value of tid.

Please respond because I am stuck

#1

gjfreakout - September 5, 2007 - 01:59

actually i meant $node->field_state[0]['tid']

#2

robertDouglass - September 5, 2007 - 21:27

I don't know enough about computed field to know what is in scope when, but can you execute PHP on the $node object and do a print_r? If you can, plz post it here.

#3

gjfreakout - September 7, 2007 - 02:18

Robert

Thanks for your response.

Please note that, I am very new to Drupal as well as php. I am just trying my ways looking through other code and by hit and trial method.

Hence, I didn't understand your statement "you execute PHP on the $node object and do a print_r" Please eloborate. You'll have to tell me step by step what you are asking me to do.

Computed field is an invisible field, where you can use other field variables to generate/create/store some computed values. I believe that these values are computed when the form is submitted. For example I want to access the tid of the taxonomy field and get the term associate with it to store it directly in the table. I want to do much more but this is the simplest example.
I want to access the value of the taxonomy field. Firstly, I tried various variables which I learned in last few days. Then, I happen to discover devel module, where it shows each variable. Based on this module, I think that variable name should be $node->field_state[0]['tid']. But it is not working.

I'd really appreciate your response, because I have already wasted few days on experimenting with this.

#4

robertDouglass - September 7, 2007 - 08:40

i'm interested in finding out if the computed field module lets you execute PHP code at the point where it gets the variable from you. To find out, try entering this instead of just the variable name.

drupal_set_message(print_r($node, true));

If you're lucky you'll get all the information about the node(s) you're working with and then any questions about variable names will be answered.

#5

gjfreakout - September 8, 2007 - 22:35

Your suggestion of trying drupal_set_message(print_r($node, true)) was very helpful. Now I know what I was doing wrong.

tid value for the field can be accessed using the variable --> $node->field_state[39]['tid'] (which returns the value 39), instead of $node->field_state[0]['tid']. Here 39 is the tid of the term (state) selected.

But this variable design has a problem. to get the value (39) i need to get from the variable, i need to know the value (39). Or use a while loop, which has its own dangers. Can you suggest me some api call to get the value directly

Thanks again for your quick replies and support

#6

robertDouglass - September 9, 2007 - 08:44

If you're interested in getting the basic information about terms, the taxonomy module has several useful functions. taxonomy_get_term($tid), for example, will give you information that is pertinent to this module as well. Since you have the tids in the node you should be able to do any of the lookups you need. If you just want the first or the last tid associated with the node you can use array_pop or array_shift. Otherwise, you'll want to loop over them.

 
 

Drupal is a registered trademark of Dries Buytaert.