You are here

function _commerce_price_field_unserialize_data in Commerce Core 7

Converts saved price field data columns back to arrays for use in the rest of the current page request execution.

Parameters

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

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

2 calls to _commerce_price_field_unserialize_data()
commerce_price_field_attach_insert in modules/price/commerce_price.module
Implements hook_field_attach_insert().
commerce_price_field_attach_update in modules/price/commerce_price.module
Implements hook_field_attach_update().

File

modules/price/commerce_price.module, line 169
Defines the Price field with widgets and formatters used to add prices with currency codes to various Commerce entities.

Code

function _commerce_price_field_unserialize_data($entity_type, $entity) {

  // Loop over all the price fields attached to this entity.
  foreach (_commerce_price_get_price_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();

      // For each item in the array, unserialize or initialize its data array.
      foreach ($items as $delta => $item) {

        // If we have a non-array $item['data'], unserialize it.
        if (!empty($item['data']) && !is_array($item['data'])) {
          $entity->{$field_name}[$langcode][$delta]['data'] = unserialize($item['data']);
        }
        elseif (empty($item['data'])) {
          $entity->{$field_name}[$langcode][$delta]['data'] = array(
            'components' => array(),
          );
        }
      }
    }
  }
}