You are here

function multifield_field_delete in Multifield 7

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

Implements hook_field_delete().

File

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

Code

function multifield_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $machine_name = multifield_extract_multifield_machine_name($field);
  foreach ($items as $delta => $item) {
    $pseudo_entity = _multifield_field_item_to_entity($machine_name, $item);

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

    //$items[$delta] = _multifield_field_entity_to_item($pseudo_entity);
  }

  // Fetch all the past revisions of this entity and run all the previous
  // multifield values through hook_field_delete_revision().
  $info = entity_get_info($entity_type);
  if (!empty($info['revision table']) && !empty($info['entity keys']['revision'])) {
    $id_key = $info['entity keys']['id'];
    $entity_id = $entity->{$id_key};
    $revision_key = $info['entity keys']['revision'];
    $query = db_select($info['revision table'], 'revision');
    $query
      ->addField('revision', $revision_key);
    $query
      ->condition('revision.' . $id_key, $entity_id);
    $revision_ids = $query
      ->execute()
      ->fetchCol();
    foreach ($revision_ids as $revision_id) {
      if ($revisions = entity_load($entity_type, array(
        $entity_id,
      ), array(
        $revision_key => $revision_id,
      ))) {
        $revision = reset($revisions);
        if (!empty($revision->{$field['field_name']})) {
          foreach ($revision->{$field['field_name']} as $revision_langcode => $revision_items) {
            multifield_field_delete_revision($entity_type, $revision, $field, $instance, $langcode, $revision_items);
          }
        }
      }
    }
  }

  // Invoke hook_field_delete() 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('delete', $machine_name, $entity_type, $stub_entity, $langcode);
  if ($ids = multifield_items_extract_ids($items)) {
    entity_get_controller('multifield')
      ->resetCache($ids);
  }
}