First element of field_get_items

Public

Often used only first value of field. No need to check existing and value of [0]['value'] or [0]['uid'] each time.

Get raw version
php
  1. /**
  2.  * Fields helper
  3.  *
  4.  * Returns the first field item in the language they currently would be displayed.
  5.  *
  6.  * @param $entity_type
  7.  * The type of $entity; e.g., 'node' or 'user'.
  8.  * @param $entity
  9.  * The entity containing the data to be displayed.
  10.  * @param $field_name
  11.  * The field to be displayed.
  12.  * @param $langcode
  13.  * (optional) The language code $entity->{$field_name} has to be displayed in.
  14.  * Defaults to the current language.
  15.  * @param bool $full_data return full data of first item
  16.  * @return bool|mixed item value if available, FALSE otherwise.
  17.  */
  18. function _field_get_first_item($entity_type, $entity, $field_name, $langcode = null, $full_data = false) {
  19. if ($items = field_get_items($entity_type, $entity, $field_name, $langcode)) {
  20. //get first value of indexed array
  21. $item = reset($items);
  22. if($full_data){
  23. return $item;
  24. }
  25. //get first value from data array (uid, nid, tid, value, ...)
  26. return reset($item);
  27. }
  28. return false;
  29. }