You are here

function inline_entity_form_entity_delete in Inline Entity Form 7

Implements hook_entity_delete().

Deletes referenced entities if needed.

@todo Remove when there's a stable contrib module for this.

File

./inline_entity_form.module, line 139
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function inline_entity_form_entity_delete($entity, $type) {
  $entity_info = entity_get_info($type);
  list(, , $bundle) = entity_extract_ids($type, $entity);
  foreach (field_info_instances($type, $bundle) as $field_name => $instance) {
    if (strpos($instance['widget']['type'], 'inline_entity_form') === 0) {
      $controller = inline_entity_form_get_controller($instance);

      // The controller specified that referenced entities should be deleted.
      if ($controller && $controller
        ->getSetting('delete_references')) {
        $items = field_get_items($type, $entity, $field_name);
        if ($items) {
          $field = field_info_field($field_name);
          $ief_settings = inline_entity_form_settings($field, $instance);
          $ids = array();
          foreach ($items as $item) {
            $ids[] = $item[$ief_settings['column']];
          }
          $context = array(
            'parent_entity_type' => $type,
            'parent_entity' => $entity,
          );
          $controller
            ->delete($ids, $context);
        }
      }
    }
  }
}