You are here

function commerce_paypal_ec_form_alter in Commerce PayPal 7.2

Implements hook_form_alter().

File

modules/ec/commerce_paypal_ec.module, line 464
Implements PayPal Express Checkout in Drupal Commerce checkout.

Code

function commerce_paypal_ec_form_alter(&$form, &$form_state, $form_id) {
  if (!is_string($form_id)) {
    return;
  }

  // If we're altering a shopping cart form and PayPal EC is enabled...
  if (strpos($form_id, 'views_form_commerce_cart_form_') === 0 && commerce_paypal_ec_enabled()) {

    // If the cart form View shows line items...
    if (!empty($form_state['build_info']['args'][0]->result)) {
      $order = $form_state['order'];

      // And Express Checkout is also available as a payment option in checkout.
      if (commerce_paypal_ec_enabled($order)) {

        // Add the Express Checkout form as a suffix to the cart form.
        $payment_method = commerce_payment_method_instance_load('paypal_ec|commerce_payment_paypal_ec');
        if (!isset($payment_method['settings']['enable_on_cart']) || !empty($payment_method['settings']['enable_on_cart'])) {
          $ec_form = drupal_get_form('commerce_paypal_ec_order_form', $payment_method, $order);
          $form['#suffix'] .= drupal_render($ec_form);

          // Add the PayPal Credit form with a separator if enabled.
          if (!empty($payment_method['settings']['enable_bml'])) {
            $bml_form = drupal_get_form('commerce_paypal_bml_order_form', $payment_method, $order, TRUE);
            $form['#suffix'] .= drupal_render($bml_form);
          }
        }
      }
    }
  }

  // If we're altering a checkout form that has the PayPal EC radio button...
  if (strpos($form_id, 'commerce_checkout_form_') === 0 && !empty($form['commerce_payment']['payment_method'])) {
    $paypal_ec = FALSE;
    foreach ($form['commerce_payment']['payment_method']['#options'] as $key => &$value) {
      list($method_id, $rule_name) = explode('|', $key);

      // If we find PayPal EC, include its CSS on the form and exit the loop.
      if ($method_id == 'paypal_ec') {
        $paypal_ec = TRUE;
        $value = theme('paypal_ec_mark_image');
      }
    }

    // If we did find PayPal EC, include its CSS now.
    if ($paypal_ec) {
      $form['commerce_payment']['payment_method']['#attached']['css'][] = drupal_get_path('module', 'commerce_paypal_ec') . '/theme/commerce_paypal_ec.theme.css';
    }
  }
}