You are here

function commerce_addressbook_field_storage_pre_insert in Commerce Addressbook 7

Implements hook_field_storage_pre_insert().

This function makes sure that each bundle containing a commerce_addressbook_saved_profiles field has the value of that field set to it's own entity ID. This avoids duplication of entities. If this wouldn't be here, the commerce_addressbook_saved_profiles field might be different on bundles that have exactly the same address info, thus creating a copy instead of reusing it.

@TODO: should we also implement hook_field_storage_pre_update()?

File

./commerce_addressbook.module, line 212
:

Code

function commerce_addressbook_field_storage_pre_insert($type, $entity, &$skip_fields) {
  $profile_bundles = commerce_addressbook_get_bundles_with_field('commerce_addressbook_saved_profiles');
  list($id) = entity_extract_ids($type, $entity);

  // Look for entities & bundles that have a commerce_addressbook_saved_profiles field
  if ($profile_bundles) {
    foreach ($profile_bundles as $entity_type => $bundles) {
      if ($type == $entity_type) {
        if (in_array($entity->type, array_keys($bundles))) {
          $field = $bundles[$entity->type];

          // @TODO: is there a better way to set the value for this field?
          $entity->{$field}[LANGUAGE_NONE][0]['saved_address_profiles'] = (int) $id;
        }
      }
    }
  }
}