You are here

function _commerce_file_field_serialize_data in Commerce File 7

Converts commerce_file field data to a serialized array.

Parameters

$entity_type: The entity type variable passed through hook_field_storage_pre_*().

$entity: The entity variable passed through hook_field_storage_pre_*().

2 calls to _commerce_file_field_serialize_data()
commerce_file_field_storage_pre_insert in includes/commerce_file.field.inc
Implements hook_field_storage_pre_insert().
commerce_file_field_storage_pre_update in includes/commerce_file.field.inc
Implements hook_field_storage_pre_update().

File

includes/commerce_file.field.inc, line 204
Implement an commerce_file field, based on the file module's file field.

Code

function _commerce_file_field_serialize_data($entity_type, $entity) {

  // Loop over all the commerce_file fields attached to this entity.
  foreach (_commerce_file_get_commerce_file_fields($entity_type, $entity) as $field_name) {

    // Iterate over the items arrays for each language.
    foreach (array_keys($entity->{$field_name}) as $langcode) {
      $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();

      // Serialize data arrays before saving.
      foreach ($items as $delta => $item) {

        // Serialize an existing data array.
        if (!empty($item['data']) && is_array($item['data'])) {
          $entity->{$field_name}[$langcode][$delta]['data'] = serialize($item['data']);
        }
      }
    }
  }
}