You are here

function commerce_discount_query_entityreference_alter in Commerce Discount 7

Implements hook_query_TAG_alter().

File

./commerce_discount.module, line 1301
Defines the discount and discount offer entities, bundles and functionality.

Code

function commerce_discount_query_entityreference_alter($query) {
  $field = $query
    ->getMetaData('field');

  // Alter the default autocomplete for the compatibility selection field...
  if ($field['field_name'] == 'commerce_compatibility_selection' && strpos(current_path(), 'entityreference/autocomplete') === 0) {

    // Extract the bundle of the discount whose compatibility settings are
    // being adjusted and limit the autocomplete query to discounts of the
    // same bundle (i.e. discount type, Product vs. Order).
    $bundle_name = arg(5);
    if (in_array($bundle_name, array(
      'product_discount',
      'order_discount',
    ))) {
      $query
        ->condition('type', $bundle_name);
    }

    // Also do not include the current discount itself as an option.
    $entity_id = arg(6);
    if ($entity_id != 'NULL') {
      $query
        ->condition('discount_id', $entity_id, '!=');
    }
  }
}