Kr1
Kr1

Reputation: 1303

Programmatically get field values of term reference (Drupal 7)

How can I get a list of all the selected values ​​for a field?

For example I have a field called countries, and I want to get all countries selected for a certain node.

I need to have this list, because I want the region to be automatically selected in another field (regions), according to the chosen country.

Ex: I choose France in the countries, then automatically the European region is selected in regions

For now I can only copy the selected values ​​in the "countries" field, in the "regions" field, by doing this:

$node->field_regions = $node->field_countries;

Here is the result when I select France in the countries field :

Country: France

Regions: France

Edit: I tried with the function:field_get_items(), but all I get is an array:

$items = field_get_items('node', $node, 'field_countries', $node->language);

Something is wrong? I also tried with this but I get the same result...

$items = $node->field_countries[$node->language][0]

Upvotes: 2

Views: 7029

Answers (2)

Kr1
Kr1

Reputation: 1303

Thanks to another forum, I managed to solve my problem.

Here's how to read the content of my field countries:

foreach ($node->field_countries[$node->language] as $item) { 
    // Country id in Countries vocabulary
    $countryId = $item['tid'];
}

Upvotes: 0

Mikey P
Mikey P

Reputation: 607

Sounds like field_get_items() may be what you are looking for.

Upvotes: 1

Related Questions