You are here

function field_collection_field_insert in Field collection 7

Implements hook_field_insert().

File

./field_collection.module, line 519
Module implementing field collection field type.

Code

function field_collection_field_insert($host_entity_type, $host_entity, $field, $instance, $langcode, &$items) {
  foreach ($items as &$item) {
    if ($entity = field_collection_field_get_entity($item)) {
      if (!empty($host_entity->is_new) && empty($entity->is_new)) {

        // If the host entity is new but we have a field_collection that is not
        // new, it means that its host is being cloned. Thus we need to clone
        // the field collection entity as well.
        $new_entity = clone $entity;
        $new_entity->item_id = NULL;
        $new_entity->revision_id = NULL;
        $new_entity->is_new = TRUE;
        $entity = $new_entity;
      }
      if (!empty($entity->is_new)) {
        $entity
          ->setHostEntity($host_entity_type, $host_entity, field_collection_entity_language($host_entity_type, $host_entity), FALSE);
      }
      $entity
        ->save(TRUE);
      $item = array(
        'value' => $entity->item_id,
        'revision_id' => $entity->revision_id,
      );
      if (module_exists('pathauto')) {
        module_load_include('inc', 'field_collection', 'field_collection.pathauto');
        field_collection_pathauto_create_alias($host_entity, $entity, 'insert');
      }
    }
  }
}