You are here

function commerce_discount_field_widget_form_alter in Commerce Discount 7

Implements hook_field_widget_form_alter().

File

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

Code

function commerce_discount_field_widget_form_alter(&$element, &$form_state, $context) {
  if (isset($element['#field_name']) && ($element['#field_name'] == 'commerce_free_shipping' || $element['#field_name'] == 'commerce_percent_off_ship_serv')) {
    $element['#options'] += commerce_shipping_service_options_list();
    if ($form_state['op'] == 'edit') {
      $wrapper_offer = entity_metadata_wrapper('commerce_discount_offer', $element['#entity']);
      if ($element['#field_name'] == 'commerce_free_shipping') {
        $element['#default_value'] = $wrapper_offer->commerce_free_shipping
          ->value();
      }
      else {
        $element['#default_value'] = $wrapper_offer->commerce_percent_off_ship_serv
          ->value();
      }
    }
  }

  // If the current element is the free shipping discount strategy select list,
  // alter it to only be visible when a free shipping service has been selected.
  if (!empty($element['#field_name']) && $element['#field_name'] == 'commerce_free_shipping_strategy') {
    $element['#states'] = array(
      'invisible' => array(
        ':input[name="commerce_discount_fields[commerce_discount_offer][' . LANGUAGE_NONE . '][form][commerce_free_shipping][' . LANGUAGE_NONE . ']"]' => array(
          'value' => '_none',
        ),
      ),
    );
  }
}