You are here

function field_collection_update_7002 in Field collection 7

Remove orphaned field collection item entities.

File

./field_collection.install, line 227
Install, update and uninstall functions for the field_collection module.

Code

function field_collection_update_7002() {

  // Loop over all fields and delete any orphaned field collection items.
  foreach (field_read_fields(array(
    'type' => 'field_collection',
  )) as $field_name => $field) {
    $select = db_select('field_collection_item', 'fci')
      ->fields('fci', array(
      'item_id',
    ))
      ->condition('field_name', $field_name)
      ->condition('archived', 0);
    $select
      ->leftJoin('field_data_' . $field_name, 'field', "field.{$field_name}_value = fci.item_id ");
    $select
      ->isNull('field.entity_id');
    $ids = $select
      ->execute()
      ->fetchCol();
    entity_delete_multiple('field_collection_item', $ids);
    drupal_set_message(t('Deleted @count orphaned field collection items.', array(
      '@count' => count($ids),
    )));
  }
}