function commerce_coupon_form_commerce_discount_form_alter in Commerce Coupon 7.2
Implements hook_form_FORM_ID_alter().
File
- ./
commerce_coupon.module, line 1437 - Provides coupon functionality for Drupal Commerce.
Code
function commerce_coupon_form_commerce_discount_form_alter(&$form, &$form_state) {
// Attach a UI for adding associated coupon entities.
if (isset($form_state['triggering_element'])) {
$trigger = end($form_state['triggering_element']['#array_parents']);
}
else {
$trigger = NULL;
}
// We do not want to show the management tools if there are more than 100
// coupons attached to this discount.
$discount_wrapper = entity_metadata_wrapper('commerce_discount', $form_state['commerce_discount']);
$coupon_count = isset($form_state['coupons']) ? count($form_state['coupons']) : $discount_wrapper->coupon_count
->value();
// Establish a container.
$form['coupons'] = array(
'#type' => 'fieldset',
'#title' => t('Coupon conditions'),
'#tree' => TRUE,
'#prefix' => '<div id="commerce-coupon-discount-coupon-form">',
'#suffix' => '</div>',
'#attached' => array(
'css' => array(
drupal_get_path('module', 'commerce_coupon') . '/css/commerce_coupon.css',
),
),
);
if ($coupon_count > 100) {
// If there are too many coupons on this discount, do not try to manage
// them all on the discount page.
$form['coupons']['count_message'] = array(
'#markup' => t('Currently there are @n coupons attached to this discount. Manage them through the !ui_link', array(
'@n' => $coupon_count,
'!ui_link' => l(t('coupons UI'), 'admin/commerce/coupons'),
)),
);
return;
}
$form['#validate'][] = 'commerce_coupon_form_attach_coupons_validate';
$form['#submit'][] = 'commerce_coupon_form_attach_coupons';
// Keep track of coupons that have been added if there is a reasonably small
// amount.
if (!isset($form_state['coupons'])) {
$form_state['coupons'] = $discount_wrapper->coupons
->value();
// Keep track of these so we know which to get rid of at the end.
$form_state['original_coupons'] = $form_state['coupons'];
}
$coupons =& $form_state['coupons'];
// Handle certain triggers.
switch ($trigger) {
case 'remove_coupon':
$edit_delta = $form_state['triggering_element']['#delta'];
unset($coupons[$edit_delta]);
break;
case 'edit_coupon':
$edit_delta = $form_state['triggering_element']['#delta'];
// Store the index of the coupon that we are editing.
$form_state['edit_coupon'] = $coupons[$edit_delta];
$form_state['edit_coupon']->delta = $edit_delta;
break;
}
// Keep track of whether there is a coupon being edited currently.
if (isset($form_state['edit_coupon'])) {
$coupon = $form_state['edit_coupon'];
}
// Ajax specifications.
$ajax = array(
'callback' => 'commerce_coupon_discount_coupon_form_ajax',
'wrapper' => 'commerce-coupon-discount-coupon-form',
);
// Add new coupons.
$form['coupons']['add'] = array(
'#type' => 'button',
'#value' => t('Add new coupon'),
'#ajax' => $ajax,
'#limit_validation_errors' => array(
array(
'coupons',
'coupon_form',
),
),
);
// Find/add existing coupon.
$form['coupons']['add_existing'] = array(
'#type' => 'button',
'#value' => t('Add existing coupon'),
'#ajax' => $ajax,
'#limit_validation_errors' => array(
array(
'coupons',
'find_coupon_code',
),
),
);
if ($trigger == 'add' || isset($coupon) && !empty($coupon->is_new) && !isset($form_state['edit_coupon']->delta)) {
if (!isset($coupon)) {
$coupon = commerce_coupon_create('discount_coupon');
}
$form['coupons']['coupon_form'] = array(
'#type' => 'container',
'#parents' => array(
'coupons',
'coupon_form',
),
);
// Attach the coupon form.
commerce_coupon_attach_ajax_coupon_entity_form($form['coupons']['coupon_form'], $form_state, $coupon, $ajax, array(
array(
'coupons',
),
));
// Add responsive visibility states to the code line.
$form['coupons']['coupon_form']['code']['#states'] = array(
'disabled' => array(
'input[name="coupons[coupon_form][generate]"]' => array(
'checked' => TRUE,
),
),
);
$form['coupons']['coupon_form']['save_coupon']['#limit_validation_errors'] = array(
array(
'coupons',
'coupon_form',
),
);
$form_state['edit_coupon'] = $coupon;
}
elseif ($trigger == 'add_existing') {
$form['coupons']['find_coupon_code'] = array(
'#type' => 'textfield',
'#title' => t('Add existing coupon code'),
'#autocomplete_path' => 'commerce/coupons/find',
);
// Add existing button.
$form['coupons']['add_existing_coupon'] = array(
'#type' => 'button',
'#value' => t('Add'),
'#ajax' => $ajax,
'#limit_validation_errors' => array(
array(
'coupons',
'find_coupon_code',
),
),
);
// Cancel add existing button.
$form['coupons']['cancel_add_existing'] = array(
'#type' => 'button',
'#value' => t('Cancel'),
'#ajax' => $ajax,
'#limit_validation_errors' => array(),
);
}
// Attach coupons management table.
$form['coupons']['manage_coupons'] = array(
'#theme' => 'commerce_coupon_manage_discount_coupons',
);
if (isset($coupons)) {
foreach ($coupons as $delta => $coupon) {
$form['coupons']['manage_coupons'][$delta]['#coupon'] = $coupon;
if (isset($form_state['edit_coupon']->delta) && $delta == $form_state['edit_coupon']->delta) {
// If this one is selected, attach a coupon form.
$form['coupons']['manage_coupons'][$delta]['coupon_form'] = array(
'#type' => 'container',
'#parents' => array(
'coupons',
'manage_coupons',
$delta,
'coupon_form',
),
);
commerce_coupon_attach_ajax_coupon_entity_form($form['coupons']['manage_coupons'][$delta]['coupon_form'], $form_state, $coupon, $ajax, array(
array(
'coupons',
'manage_coupons',
$delta,
),
));
// Add responsive visibility states to the code line.
$form['coupons']['manage_coupons'][$delta]['coupon_form']['code']['#states'] = array(
'disabled' => array(
'input[name="coupons[manage_coupons][' . $delta . '][coupon_form][generate]"]' => array(
'checked' => TRUE,
),
),
);
}
else {
$form['coupons']['manage_coupons'][$delta]['edit_coupon'] = array(
'#type' => 'button',
'#ajax' => $ajax,
'#value' => t('edit'),
'#limit_validation_errors' => array(),
'#delta' => $delta,
'#name' => 'edit-coupon-' . $delta,
);
$form['coupons']['manage_coupons'][$delta]['remove_coupon'] = array(
'#type' => 'button',
'#ajax' => $ajax,
'#value' => t('remove'),
'#limit_validation_errors' => array(),
'#delta' => $delta,
'#name' => 'remove-coupon-' . $delta,
);
}
}
}
else {
// This means there are too many coupons for the management widget to
// handle. Provide a summary.
$form['coupons']['coupons_summary'] = array(
'#theme' => 'commerce_coupon_discount_coupons_summary',
'#count' => $coupon_count,
);
}
// Clear the input if save was successful.
unset($form_state['values']['coupons']['coupon_form']);
unset($form_state['input']['coupons']['coupon_form']);
$form_state['coupons'] = $coupons;
}