Hi, I have a vocabulary called "Style Colors". And in my nodes, I have a field referencing it called "field_stylecolor".

When I have,

$tid= $entity->field_stylecolor['und'][0];
$entity_field[0]['value'] = $tid;

in my computed field, it returns "34", which is the tid of a color (e.g. "red").

Now, I want the computed field to return the (string) term name of the color (i.e. "red"), so I type,

$tid = $entity->field_stylecolor['und'][0];
$term = taxonomy_term_load($tid);
$entity_field[0]['value'] = $term->name;

in computed field, but now it doesn't return anything.

What am I doing wrong here?

Comments

Anonymous’s picture

Try this..

$term = $entity->field_stylecolor['und'][0]['tid'];
$term = taxonomy_term_load($term);
$entity_field[0]['value'] = $term->name;
doublejosh’s picture

Don't think this is Computed Field's fault. I've found the same problem with taxonomy_term_load elsewhere. In a custom menu callback I can access a profile field and get the tid, but get nothing from the taxonomy load function.

function mymodule_menu() {
  $items = array();
  $items['user/account-convert'] = array(
    'title' => 'My Page',
    'page callback' => 'mymodule_page',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function mymodule_page() {
  global $user;
  $profile = profile2_load_by_user($user, 'my_profile_type');
  // Sanity check tid return. Success.
  echo $profile->field_profile_myfield[LANGUAGE_NONE][0]['tid'];
  // Term name returns nothing. The object just won't load.
  echo taxonomy_term_load($profile->field_profile_myfield[LANGUAGE_NONE][0]['tid'])->name;
}

WTF!

Anonymous’s picture

The code I posted above seems to work in Computed Field. In regular Drupal files I use something like this:

  $profile = profile2_load_by_user($user, 'my_profile_type');
  $term=taxonomy_term_load($profile->field_stylecolor['und'][0]['tid']);
  print $term->name;
dos360’s picture

Issue summary: View changes

Kline's code is worked for me. Here is my example where $coll stands the collection name stored as taxonomy terms and $number denotes the individual document's id.

$coll = $entity->field_collection_name[LANGUAGE_NONE][0]['tid'];
$coll_obj = taxonomy_term_load($coll);
$number = $entity->field_document_number[LANGUAGE_NONE][0]['value'];
$entity_field[0]['value'] = "$coll_obj->name $number";
kaizerking’s picture

I have profile2 profile,
which has the filed collection field field_resume_xp, unlimted values,this field collection field has a field called 'field_duration' this value is populated with computed field
This field collection also has a term reference field with two terms ]term_1 and term_2

Now I have 2 fields in profile2 profile "field_total_duration_term1" and "field_total_duration_term2" I want the sum of all the "field_duration" values for term_1 in "field_total_duration_term1" field and sum of all the "field_duration" values for term|_2 in "field_total_duration_term2" field
Simply I want sum of term1 durations and sum of term2 durations separately computed and stored
how to get this?

without applying the term filter this code works very nicely to get sum of field durations probably some one can help me with filetering by term

$sum = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_resume_xp as $collection) {

   $sum += $collection->field_duration->value();
}

$entity_field[0]['value'] = $sum;
kaizerking’s picture

What am I doin wrong in this code ?

$sum = 0;
$wrapper = entity_metadata_wrapper($entity_type, $entity);
foreach($wrapper->field_resume_xp as $collection) {
$tid= $collection->field_resume_job_area->value()->tid ;
foreach($tid as as $collection_item){
$collection_item = $collection->field_duration->value();
if ($collection_item->field_resume_job_area->value()->tid) = 625;
$sum += $collection_item->field_duration->value();
}
}
$entity_field[0]['value'] = $sum;
matthewgann’s picture

I also can't get taxonomy_term_load function to work within computed field. Trying to grab field data from a term from the author.

Any idea on a workaround or a fix?