One can retrieve field items from an entity with

// Find all terms on $node in field 'field_tags'.
$terms = field_get_items('node', $node, 'field_tags');

But is there a convenient way to do the reverse, Core, or otherwise?

// Attach new term $term to $node in field 'field_tags'.
$term = taxonomy_term_load($tid);
$node = field_set_items('node', $node, 'field_tags', 'taxonomy_term', $term);

I would like to avoid the hassle of manually assigning a $term, go look in devel what the array structure looks like and then build the array by hand, as it is sometimes non-trivial.

The only issue in the core queue I could find that looked vaguely similar was about the field language api DX.

3 Answers 3


No, there is no such function.

See my comment at Language support for entity fields.

  • 1
    Thanks Berdir. I was hoping you'd show up ;) and I was also afraid you'd say so. The problem with the code in your comment is that for a lot of fields, 'value' may be insufficient or just plain wrong.
    – Letharion
    Commented Apr 12, 2012 at 10:46
  • field_get_items() does not help you with that either, it returns an array of items that contain value. Or whatever the field type defines. There is no way to abstract that.
    – Berdir
    Commented Apr 12, 2012 at 20:08
  • For example, field_get_items() returns array(array('tid' => 123)). Your code needs to know that field_tags is a taxonomy term reference and that it has the tid key set and that it is a teference to a taxonomy term.
    – Berdir
    Commented Apr 13, 2012 at 9:59
  • The comments are no longer visible at that page as per given link.
    – Junaid
    Commented Nov 9, 2016 at 8:52
  • Comments are on the discuss page now: drupal.org/node/1500308/discuss
    – Berdir
    Commented Nov 9, 2016 at 13:18

For such a function to exist, field_set_items() would need to be able to identify the stdClass object as that of a taxonomy term, and also map it to a function that can decode the object and create a field array out of it.

Since there's no core function to do that, I don't think this will be easy.

  • It's terribly irritating that there's no $entity->entity_type property (yet), but regardless, that could easily be added to my example callback. I updated the question to reflect this.
    – Letharion
    Commented Apr 12, 2012 at 10:51
  • 1
    The lack of an entity_type isn't really the problem IMHO...the problem is exactly what you've added to your question: "it is sometimes non-trivial". Because the data is non-trivial, for field_set_items to work as you desire, each field type in the system would need to implement some sort of interface describing how to reverse-engineer the data array from an object/arbitrary variable.
    – Clive
    Commented Apr 12, 2012 at 11:05

Although this doesn't help building the some times complicated array structure, I see that what I'm looking for has to some extent been introduced in Drupal 8. Perhaps it will be extended further in the future. :)

New simpler API available for accessing fields, properties and language information.

Example usage from the linked page:

// Change a field value.
$body = $comment->get('comment_body');
$body[0]['value'] = 'This is actually a perverted tango';
$comment->set('comment_body', $body);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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