You are here

function relation_entity_delete in Relation 7

Same name and namespace in other branches
  1. 8.2 relation.module \relation_entity_delete()
  2. 8 relation.module \relation_entity_delete()

Implements hook_entity_delete().

File

./relation.module, line 965
Describes relations between entities.

Code

function relation_entity_delete($entity, $entity_type) {
  list($entity_id) = entity_extract_ids($entity_type, $entity);
  $relations = relation_query($entity_type, $entity_id)
    ->execute();
  $relations_to_delete = array();
  foreach ($relations as $row) {

    // Remove any endpoints pointing to entity
    $relation = relation_load($row->rid);
    foreach ($relation->endpoints[LANGUAGE_NONE] as $key => $endpoint) {
      if ($endpoint['entity_id'] == $entity_id && $endpoint['entity_type'] == $entity_type) {
        unset($relation->endpoints[LANGUAGE_NONE][$key]);
      }
    }

    // Check if relation remains valid with regards to arity
    $type = relation_get_types(array(
      $relation->relation_type,
    ));
    $min_arity = $type[$relation->relation_type]->min_arity;
    $arity = count($relation->endpoints[LANGUAGE_NONE]);
    if ($arity < $min_arity) {

      // Not valid - delete
      array_push($relations_to_delete, $relation->rid);
    }
    else {

      // Valid - save
      relation_save($relation);
    }
  }
  if (!empty($relations_to_delete)) {
    relation_delete_multiple($relations_to_delete);
    drupal_set_message(t('Relations @relations have been deleted.', array(
      '@relations' => implode(', ', $relations_to_delete),
    )));
  }
}