1 file.field.inc file_field_expand_items(array $items)

Expands multiple file IDs within a single item into separate item arrays.

When using a multiple-value file field, an individual $item['fid'] may contain an array of file IDs. Before saving values, each $item['fid'] should only contain an integer. This helper converts multiple file IDs within a single item into separate items.

Calling this function is only needed when new files may be added, such as on insert or update operations.

Parameters

array $items:

Return value

array: The modified list of $items.

File

core/modules/file/file.field.inc, line 428
Field module functionality for the File module.

Code

function file_field_expand_items(array $items) {
  foreach ($items as $i => $item) {
    if (is_array($item['fid'])) {
      $multiple_fids = $item['fid'];
      $new_items = array();
      foreach ($multiple_fids as $f => $fid) {
        // The first item should adopt the first file ID.
        if ($f === 0) {
          $items[$i]['fid'] = $fid;
        }
        // Subsequent file IDs duplicate the first item's properties.
        else {
          $item['fid'] = $fid;
          $new_items[] = $item;
        }
      }
      // Insert the new items in the array after the original item.
      array_splice($items, $i + 1, 0, $new_items);
    }
  }

  return $items;
}