You are here

function commerce_entity_reference_delete in Commerce Core 7

Deletes a reference to another entity from an entity with a reference field.

Parameters

$entity: The entity that contains the reference field.

$field_name: The name of the entity reference field.

$col_name: The name of the column in the field's schema containing the referenced entity's ID.

$ref_entity_id: The ID of the entity to delete from the reference field.

5 calls to commerce_entity_reference_delete()
CommerceLineItemEntityController::delete in modules/line_item/includes/commerce_line_item.controller.inc
Delete permanently saved line items.
commerce_cart_order_product_line_item_delete in modules/cart/commerce_cart.module
Deletes a product line item from a shopping cart order.
commerce_customer_commerce_customer_profile_delete in modules/customer/commerce_customer.module
Implements hook_commerce_customer_profile_delete().
commerce_line_item_delete_references in modules/line_item/commerce_line_item.module
Deletes any references to the given line item.
commerce_product_reference_commerce_product_delete in modules/product_reference/commerce_product_reference.module
Implements hook_commerce_product_delete().

File

./commerce.module, line 125
Defines features and functions common to the Commerce modules.

Code

function commerce_entity_reference_delete($entity, $field_name, $col_name, $ref_entity_id) {

  // Exit now if the entity does not have the expected field.
  if (empty($entity->{$field_name})) {
    return;
  }

  // Loop over each of the field's items in every language.
  foreach ($entity->{$field_name} as $langcode => $items) {
    $rekey = FALSE;
    foreach ($items as $delta => $item) {

      // If the item references the specified entity...
      if (!empty($item[$col_name]) && $item[$col_name] == $ref_entity_id) {

        // Unset the reference.
        unset($entity->{$field_name}[$langcode][$delta]);
        $rekey = TRUE;
      }
    }
    if ($rekey) {

      // Rekey the field items if necessary.
      $entity->{$field_name}[$langcode] = array_values($entity->{$field_name}[$langcode]);

      // If the reference field is empty, wipe its data altogether.
      if (count($entity->{$field_name}[$langcode]) == 0) {
        unset($entity->{$field_name}[$langcode]);
      }
    }
  }
}