0

The users in my webapplication have a term reference suppliers.

I also have a content type transaction where I have the same term reference. Now I would like to hide the select list and set the default value programmatically. This is what I do now:

$form['field_supplier']['#type'] = 'hidden';
$user_suppliers = array();

foreach($user_selling->field_suppliers['und'] as $supplier)
{
    $user_suppliers[] = $supplier['tid'];
}

$firstsupplier = reset($user_suppliers);

$first_supplier = taxonomy_term_load($firstsupplier);

As you can see I loop through the tid's and save them in array. Afterwards I pick the first one (there's a reason I don't pick the first instead of looping through).

The $firstsupplier variable results 7 so what's not abnormal for a tid. But when I want to load the taxonomy term I get a response false... (he can't find it) .

I recently resetted my taxonomy vocabulary to alphabetical, could that be the reason? (I think not, because I tried it after saving them in a random order)

1

First, don't use 'und', you should be using LANGUAGE_NONE instead.

Your code works, so you might have a data inconsistency on your database, are you sure that the term id 7 really exists?

You can use entity api metadata wrappers to access easier to the field information, or if you're not using Entity API module, the function field_get_items is a better way to access the information.

$user_suppliers = array();
$items = field_get_items('node', $user_selling, 'field_suppliers');

foreach($items as $item) {
    $user_suppliers[] = $item['tid'];
}

$firstsupplier = reset($user_suppliers);
$first_supplier = taxonomy_term_load($firstsupplier);
| improve this answer | |
  • When I load the items I get the same result (same tids). When I check for them in the database they're not there. (The tid's should be different ... The supplier is in the database but wrong tid) – nielsv Jun 17 '14 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.