function commerce_sagepay_form_alter in Drupal Commerce SagePay Integration 7
Implements hook_form_alter().
Adjust the credit card area of the payment form to include the card logos of active card types.
File
- ./
commerce_sagepay.module, line 611
Code
function commerce_sagepay_form_alter(&$form, &$form_state, $form_id) {
// If the current form ID is for a checkout form...
if (strpos($form_id, 'commerce_checkout_form_') === 0) {
// Check if the payment pane is on this page - return if not.
if (!isset($form['commerce_payment'])) {
return;
}
// Check if this is the direct payment method.
// This is the only one that needs card logos.
$instance_id = $form['commerce_payment']['payment_method']['#default_value'];
$payment_method = commerce_payment_method_instance_load($instance_id);
if (strpos($payment_method['instance_id'], 'sagepay_direct') > 0) {
drupal_add_css(drupal_get_path('module', 'commerce_sagepay') . '/css/sagepay_direct.css');
// And it specifies a valid checkout page...
if (commerce_checkout_page_load(substr($form_id, 23))) {
// And the current page's form includes the payment checkout pane...
if (!empty($form['commerce_payment'])) {
// get a list of the currently enabled cards
$enabled_cards = _commerce_sagepay_populate_card_names(variable_get(SAGEPAY_SETTING_ENABLED_CARDS));
// if there are nio enabled cards, then get all available cards
if (empty($enabled_cards)) {
$enabled_cards = _commerce_sagepay_all_card_names();
}
$card_images = theme('commerce_sagepay_card_logos', array(
'enabled_cards' => $enabled_cards,
));
$payment_details = $form['commerce_payment']['payment_details'];
if (array_key_exists('credit_card', $payment_details)) {
// Add credit card logos about the card dropdown.
array_unshift($form['commerce_payment']['payment_details']['credit_card'], array(
'logos' => array(
'#markup' => $card_images,
),
));
// Replace the card selection dropdown with one based on our
// available cards.
$form['commerce_payment']['payment_details']['credit_card']['type']['#options'] = $enabled_cards;
// Disable compulsory start date field as not every card has a
// start date.
$empty_option = array(
0 => '-',
);
$start_months = $form['commerce_payment']['payment_details']['credit_card']['start_month']['#options'];
$start_years = $form['commerce_payment']['payment_details']['credit_card']['start_year']['#options'];
$form['commerce_payment']['payment_details']['credit_card']['start_month']['#options'] = $empty_option + $start_months;
$form['commerce_payment']['payment_details']['credit_card']['start_month']['#default_value'] = 0;
$form['commerce_payment']['payment_details']['credit_card']['start_month']['#required'] = FALSE;
$form['commerce_payment']['payment_details']['credit_card']['start_year']['#options'] = $empty_option + $start_years;
$form['commerce_payment']['payment_details']['credit_card']['start_year']['#default_value'] = 0;
$form['commerce_payment']['payment_details']['credit_card']['start_year']['#required'] = FALSE;
}
}
}
}
}
}