You are here

function payment_form_process_method in Payment 7

Implements form process callback for a payment_method element.

See also

payment_element_info()

1 string reference to 'payment_form_process_method'
payment_element_info in ./payment.module
Implements hook_element_info().

File

./payment.ui.inc, line 902
The Payment user interface.

Code

function payment_form_process_method(array $element, array &$form_state, array &$form) {

  /** @var Payment $payment */
  $payment = $form_state['payment'];
  $element['#tree'] = TRUE;

  // Get available payment methods.
  $pmid_options = array();
  $pmids = empty($element['#pmids']) ? FALSE : $element['#pmids'];
  foreach ($payment
    ->availablePaymentMethods(entity_load('payment_method', $pmids)) as $payment_method) {

    // Cast the PMID to a string or the AJAX callback won't work.
    $pmid_options[(string) $payment_method->pmid] = check_plain($payment_method->title_generic);
  }

  // There are no available payment methods.
  if (count($pmid_options) == 0) {
    if (!$payment->pid) {
      $form['#disabled'] = TRUE;
    }
    $element['pmid_title'] = array(
      '#type' => 'item',
      '#title' => isset($element['#title']) ? $element['#title'] : NULL,
      '#markup' => t('There are no available payment methods.'),
    );
  }
  elseif (count($pmid_options) == 1) {
    $pmids = array_keys($pmid_options);
    $element['pmid'] = array(
      '#type' => 'value',
      '#value' => $pmids[0],
    );
    if (isset($element['#title'])) {
      $element['pmid_title'] = array(
        '#type' => 'item',
        '#title' => $element['#title'],
        '#markup' => $pmid_options[$pmids[0]],
      );
    }

    // Default to the only available payment method.
    if (!$payment->method || $payment->method->pmid != $pmids[0]) {
      $payment->method = entity_load_single('payment_method', $pmids[0]);
    }
    $element['payment_method_controller_payment_configuration'] = array(
      '#type' => 'payment_form_context',
      '#payment_method_controller_name' => $payment_method->controller->name,
      '#callback_type' => 'payment',
    );
  }
  else {
    natcasesort($pmid_options);
    $form['#prefix'] = '<div id="payment-method-wrapper">';
    $form['#suffix'] = '</div>';
    $element['pmid'] = array(
      '#type' => 'select',
      '#title' => isset($element['#title']) ? $element['#title'] : NULL,
      '#options' => $pmid_options,
      '#default_value' => isset($payment->method) ? $payment->method->pmid : NULL,
      '#empty_value' => 'select',
      '#required' => $element['#required'],
      '#ajax' => array(
        'callback' => 'payment_form_process_method_submit_ajax_callback',
        'effect' => 'fade',
        'event' => 'change',
        'wrapper' => 'payment-method-wrapper',
      ),
      // Disable the selector for non-JS pages. This means that if we're
      // executing an AJAX callback, _triggering_element_name is set and we leave
      // the element enabled.
      '#disabled' => !empty($payment->method) && !isset($form_state['input']['_triggering_element_name']),
      '#attached' => array(
        'js' => array(
          drupal_get_path('module', 'payment') . '/js/payment.js',
        ),
      ),
      '#id' => 'payment-method-pmid',
    );
    if ($payment->method) {
      $element['change'] = array(
        '#type' => 'submit',
        '#value' => t('Change payment method'),
        '#submit' => array(
          'payment_form_process_method_submit',
        ),
        '#limit_validation_errors' => array(),
        '#attributes' => array(
          'class' => array(
            'js-hide',
          ),
        ),
      );
      $element['payment_method_controller_payment_configuration'] = array(
        '#type' => 'payment_form_context',
        '#payment_method_controller_name' => $payment->method->controller->name,
        '#callback_type' => 'payment',
      );
    }
  }

  // The element itself has no input, only its children, so mark it not
  // required to prevent validation errors.
  $element['#required'] = FALSE;
  return $element;
}