You are here

function commerce_product_reference_form_commerce_product_product_delete_form_alter in Commerce Core 7

Implements hook_form_FORM_ID_alter().

When a product is being deleted. Display a message on the confirmation form saying how many times the product is referenced in all product reference fields.

File

modules/product_reference/commerce_product_reference.module, line 229
Defines a field type for referencing products from other entities.

Code

function commerce_product_reference_form_commerce_product_product_delete_form_alter(&$form, &$form_state) {
  $items = array();

  // Check the data in every product reference field.
  foreach (commerce_info_fields('commerce_product_reference') as $field_name => $field) {

    // Query for any entity referencing the deleted product in this field.
    $query = new EntityFieldQuery();
    $query
      ->fieldCondition($field_name, 'product_id', $form_state['product']->product_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) {
        if (($count = count($data)) > 0) {

          // For line item references, display a message about the inability of
          // the product to be deleted and disable the submit button.
          if ($entity_type == 'commerce_line_item') {

            // Load the referencing line item.
            $line_item = reset($data);
            $line_item = commerce_line_item_load($line_item->line_item_id);

            // Implement a soft dependency on the Order module to show a little
            // more information in the non-deletion message.
            if (!empty($line_item->order_id) && ($order = commerce_order_load($line_item->order_id))) {
              $description = t('This product is referenced by a line item on Order @order_number and therefore cannot be deleted. Disable it instead.', array(
                '@order_number' => $order->order_number,
              ));
            }
            else {
              $description = t('This product is referenced by a line item and therefore cannot be deleted. Disable it instead.');
            }
            $form['description']['#markup'] .= '<p>' . $description . '</p>';
            $form['actions']['submit']['#disabled'] = TRUE;
            return;
          }

          // Load the entity information.
          $entity_info = entity_get_info($entity_type);

          // Add a message regarding the references.
          $items[] = t('@entity_label: @count', array(
            '@entity_label' => $entity_info['label'],
            '@count' => $count,
          ));
        }
      }
    }
  }
  if (!empty($items)) {
    $form['description']['#markup'] .= '<p>' . t('This product is referenced by the following entities: !entity_list', array(
      '!entity_list' => theme('item_list', array(
        'items' => $items,
      )),
    )) . '</p>';
  }
}