Experimental project

This is a sandbox project, which contains experimental code for developer use only.

Provides field_get_value(), field_get_values(), and field_set_values() functions for developer and themer use.

Developers and themers expect to constantly type some form of $entity->field_data['und'][0]['value']; throughout site customization modules and themes. It gets tedious, the code can be difficult to read, and you find yourself staring at the output of devel's dsm() far too much.

Install this and you can use field_get_value($entity, 'data'); Note the missing 'field_' name prefix, it is automatically added as needed. Multi-value fields are converted to/from standard arrays, which can save a lot of array processing code. field_set_values() accepts values or arrays, so field_set_value() would be redundant. Note: Fields with multiple columns should be accessed with field_get_items().

//This "get value" code:
  print $entity->field_data['und'][0]['value'];
//Becomes:
  print field_get_value($entity, 'data');
//This "get values" code:
  foreach ($entity->field_data['und'] as $value) {
    print $value['value'];
  }
//Or this "get values using field_get_items" code:
  $items = field_get_items('node', $entity, 'field_data');
  foreach ($items as $value) {
    print $value['value'];
  }
//Becomes:
  $values = field_get_values($entity, 'data');
  foreach ($values as $value) {
    print $value;
  }
//And possibly my favorite, this "set values" code:
  $entity->field_data['und'] = array();
  foreach ($values as $delta => $value) {
    $entity->field_data['und'][] = array('value' => $value);
  }
//Becomes:
  field_set_values($entity, 'data', $values);

I am not recommending using print() during data processing. The above code is for example only. Use preprocess functions!

You may find the entity_metadata_wrapper() function more useful depending on your needs: http://drupalcontrib.org/api/drupal/contributions--entity--entity.module... Check it out also.

I'm on the fence about making this into a full project. Let me know if you find it useful, and I'll make it a full project. Hopefully, something like it can be included in D8.

Project information