You are here

function commerce_discount_form_commerce_discount_form_alter in Commerce Discount 7

Implements hook_form_FORM_ID_alter().

Disable alteration of discounts with overridden rules.

File

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

Code

function commerce_discount_form_commerce_discount_form_alter(&$form, $form_state) {

  // Add clearfix to the discount type container.
  $form['commerce_discount_type']['#attributes']['class'][] = 'clearfix';
  $form['commerce_discount_fields']['commerce_discount_offer']['#attributes']['class'][] = 'clearfix';
  if (empty($form_state['commerce_discount']->discount_id)) {

    // Entity is new.
    return;
  }
  if (!empty($form_state['triggering_element']['#ajax'])) {

    // We are inside an Ajax call.
    return;
  }
  $rule = rules_config_load('commerce_discount_rule_' . $form_state['commerce_discount']->name);
  if ($rule && $rule
    ->hasStatus(ENTITY_OVERRIDDEN)) {
    drupal_set_message(t('The rule associated with this discount is overridden, making it impossible to edit the discount.'), 'warning');
    $form['actions']['submit']['#disabled'] = TRUE;
  }
}