You are here

function paragraphs_field_delete in Paragraphs 7

Implements hook_field_delete().

File

./paragraphs.module, line 795
Paragraphs hooks and common functions.

Code

function paragraphs_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
  if ($field['type'] == 'paragraphs') {

    // Also delete all embedded entities.
    if ($ids = paragraphs_field_item_to_ids($items)) {

      // We filter out entities that are still being referenced by other
      // host-entities. This should never be the case, but it might happen e.g.
      // when modules clone a node without knowing about paragraphs.
      $entity_info = entity_get_info($entity_type);
      $entity_id_name = $entity_info['entity keys']['id'];
      $field_column = key($field['columns']);

      // Extra check to make sure our field exists.
      if (is_scalar($field)) {
        $field_definition = field_info_field($field['field_name']);
        if (!empty($field_definition)) {
          foreach ($ids as $id_key => $id) {
            $query = new EntityFieldQuery();
            $entities = $query
              ->fieldCondition($field['field_name'], $field_column, $id)
              ->execute();
            unset($entities[$entity_type][$entity->{$entity_id_name}]);
            if (!empty($entities[$entity_type])) {

              // Filter this $id out.
              unset($ids[$id_key]);
            }
          }
        }
      }
      entity_delete_multiple('paragraphs_item', $ids);
    }
  }
}