You are here

function multifield_field_update in Multifield 7

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

Implements hook_field_update().

File

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

Code

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

  // Gather the original field values from the parent entity if it has them.
  $original_items = !empty($entity->original->{$field['field_name']}[$langcode]) ? $entity->original->{$field['field_name']}[$langcode] : array();
  foreach ($items as $delta => $item) {
    $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);

    // Add the original item value to the pseudo-entity.
    if (!empty($original_items[$delta])) {
      $pseudo_entity->original = _multifield_field_item_to_entity($machine_name, $original_items[$delta]);
    }

    // Ensure the revision flag is set.
    if (!empty($entity->revision)) {
      $pseudo_entity->revision = TRUE;
    }

    // Run each sub-field through hook_field_update().
    _multifield_field_invoke('update', $machine_name, 'multifield', $pseudo_entity, $langcode);

    // Run each multifield pseudo-entity through hook_field_storage_pre_update().
    // @todo This invocation should probably be moved to a multifield_field_storage_pre_update().
    $skip_fields = array();
    foreach (module_implements('field_storage_pre_update') as $module) {
      $function = $module . '_field_storage_pre_update';
      $function('multifield', $pseudo_entity, $skip_fields);
    }
    unset($pseudo_entity->original);
    $items[$delta] = _multifield_field_entity_to_item($pseudo_entity);
  }

  // Invoke hook_field_update() 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, $original_items, $entity_type, $entity, $langcode);
  _multifield_field_invoke('update', $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);
}