The function field_get_items is supposed to only return an array or FALSE, however on php 5.3 it can return a string.
For reference, I'm running on a 64 bit ubuntu server 12.04 with php 5.3.10 from the repo. This is not based on internal drupal behavior, and we can look at an example without bootstrapping drupal.
<?php
$field_name = 'body';
$entity = new stdClass();
$entity->{$field_name} = 'Stub body';
$langcode = 'en';
var_dump(isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : FALSE);
?>
The code above on php 5.3.10 returns 'S'.
Therefore, the function field_get_items will return a string instead of an array or false.
However, the return value for function field_get_items is stated as "An array of field items keyed by delta if available, FALSE otherwise". This means that either the function field_get_items or the return value is stated incorrectly. I believe it's incorrect and unexpected behavior for function field_get_items to return a string. Therefore I propose the function field_get_items look like the following.
<?php
/**
* Returns the field items in the language they currently would be displayed.
*
* @param $entity_type
* The type of $entity; e.g., 'node' or 'user'.
* @param $entity
* The entity containing the data to be displayed.
* @param $field_name
* The field to be displayed.
* @param $langcode
* (optional) The language code $entity->{$field_name} has to be displayed in.
* Defaults to the current language.
*
* @return
* An array of field items keyed by delta if available, FALSE otherwise.
*/
function field_get_items($entity_type, $entity, $field_name, $langcode = NULL) {
$langcode = field_language($entity_type, $entity, $field_name, $langcode);
if (isset($entity->{$field_name}[$langcode]) && is_array($entity->{$field_name}[$langcode])) {
return $entity->{$field_name}[$langcode];
}
return FALSE;
}
?>
I've added a patch based on the latest 7.x branch.
Attachment | Size | Status | Test result | Operations |
---|---|---|---|---|
field-function_field_get_items_can_return_string.patch | 705 bytes | Idle | PASSED: [[SimpleTest]]: [MySQL] 40,351 pass(es). | View details | Re-test |