Easiest way to get term entities from a term reference field? I always end up with lots of juggling to get seemingly simple things, like "All term names".

$field_terms = field_get_items($entity_type, $entity, $tags_field);

if ($field_terms) {
  $tids = array();
  foreach ($field_terms as $field_term) {
    $tids[] = $field_term['tid'];
  }
  $terms = taxonomy_term_load_multiple($tids);

  $tags = '';
  foreach ($terms as $term) {
    $tags .= $term-&gt;name . ',';
  }
  $tags = rtrim($tags, ',');
}


Can anyone share any neat tricks for making the above easier?

The part that really annoys me is the first loop, that I need just to untangle the field api array.
This one annoys the bejesus out of me too. Things are slightly nicer using the entity API module, but it's still not pretty.

I've resorted to including a helper function in most projects now to extract an array of values for a single field column:

function _field_get_column_values($entity_type, $entity, $field_name, $column_name) {
  if ($field_items = field_get_items($entity_type, $entity, $field_name)) {
    $values = array();
    foreach ($field_items as $item) {
      if (isset($item[$column_name])) {
        $values[] = $item[$column_name];
      }
    }
    return empty($values) ? FALSE : $values;
  }

  return FALSE;
}


Using that your code would shrink a little:

if ($tids = _field_get_column_values($entity_type, $entity, $tags_field, 'tid')) {
  $terms = taxonomy_term_load_multiple($tids);

  // ...
}


It's still not a great solution, but I find it saves a fair bit of time on projects that need a lot of custom code.