You are here

function _commerce_file_license_line_item_delete_references in Commerce File 7

Deletes any references to the given line item on a commerce file license. @ref commerce_line_item_delete_references()

1 call to _commerce_file_license_line_item_delete_references()
commerce_file_commerce_order_delete in ./commerce_file.module
Implements hook_commerce_order_delete().

File

includes/commerce_file.entities.inc, line 1167
Handles file licenses and file license logs

Code

function _commerce_file_license_line_item_delete_references($line_item) {
  $processed =& drupal_static(__FUNCTION__, array());
  if (empty($line_item->line_item_id) || isset($processed[$line_item->line_item_id])) {
    return;
  }

  // mark as processed
  $processed[$line_item->line_item_id] = TRUE;

  // keep track of updated entities
  $updated = array();

  // Check the data in every line item reference field.
  foreach (commerce_info_fields('commerce_line_item_reference', COMMERCE_FILE_LICENSE_ENTITY_NAME) as $field_name => $field) {

    // Query for any entity referencing the deleted line item in this field.
    $query = new EntityFieldQuery();
    $query
      ->fieldCondition($field_name, 'line_item_id', $line_item->line_item_id, '=');
    $result = $query
      ->execute();

    // If results were returned...
    if (!empty($result)) {

      // Loop over results for each type of entity returned.
      foreach ($result as $entity_type => $data) {

        // Load the entities of the current type.
        $entities = entity_load($entity_type, array_keys($data));

        // Loop over each entity and remove the reference to the deleted line item.
        foreach ($entities as $entity_id => $entity) {
          commerce_entity_reference_delete($entity, $field_name, 'line_item_id', $line_item->line_item_id);
          entity_save($entity_type, $entity);
          $updated[] = $entity_id;
        }
      }
    }
  }
  return $updated;
}