You are here

function multifield_field_insert in Multifield 7

Same name and namespace in other branches
  1. 7.2 multifield.field.inc \multifield_field_insert()

Implements hook_field_insert().

File

./multifield.field.inc, line 324
Field integration for the Multifield module.

Code

function multifield_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $machine_name = multifield_extract_multifield_machine_name($field);

  // Because field default values are loaded after hook_field_presave() is run,
  // manually call it again if a default value was used.
  if (!empty($instance['default_value']) && $items === $instance['default_value']) {

    // @todo Revisit why this is necessary and if it could be simplified or removed.
    multifield_field_presave($entity_type, $entity, $field, $instance, $langcode, $items);
  }

  // Run each sub-field through hook_field_insert().
  foreach ($items as $delta => $item) {
    $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);

    // Run each sub-field through hook_field_insert().
    _multifield_field_invoke_default('insert', $machine_name, 'multifield', $pseudo_entity, $langcode);
    _multifield_field_invoke('insert', $machine_name, 'multifield', $pseudo_entity, $langcode);

    // Run each multifield pseudo-entity through hook_field_storage_pre_insert().
    // @todo This invocation should probably be moved to a multifield_field_storage_pre_insert().
    $skip_fields = array();
    foreach (module_implements('field_storage_pre_insert') as $module) {
      $function = $module . '_field_storage_pre_insert';
      $function('multifield', $pseudo_entity, $skip_fields);
    }
    $items[$delta] = _multifield_field_entity_to_item($pseudo_entity);
  }

  // Invoke hook_field_insert() with the sub-field data attached to a stub of
  // the parent entity.
  $stub_entity = _multifield_create_stub_entity_with_subfield_data($machine_name, $items, array(), $entity_type, $entity, $langcode);
  _multifield_field_invoke('insert', $machine_name, $entity_type, $stub_entity, $langcode);

  // Because this is invoked right prior to field storage writing, we need to
  // re-serialize the field values.
  array_walk($items, 'multifield_item_serialize', $machine_name);
  multifield_update_maximum_id($items);
}