You are here

function payment_form_process_line_item in Payment 7

Implements form process callback for a payment_line_item element.

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

File

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

Code

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

  // Fetch all line items to display elements for.
  $line_items = array();
  if (isset($element['#default_value'])) {
    $line_items = array_values($element['#default_value']);
  }

  // Apply cardinality limit.
  if ($element['#cardinality'] > 0) {
    $line_items = array_slice($line_items, 0, $element['#cardinality']);
  }
  if (!isset($form_state['payment_line_item_count'])) {
    $form_state['payment_line_item_count'] = max(1, count($line_items));
  }

  // Add any tracked line items that haven't been added yet.
  if ($form_state['payment_line_item_count'] > count($line_items)) {
    $diff = $form_state['payment_line_item_count'] - count($line_items);
    $line_items = array_merge($line_items, array_fill(0, $diff, NULL));
  }

  // Build the line items.
  foreach ($line_items as $delta => $line_item) {
    $required = $delta == 0 && $element['#required'];
    $element['container_' . $delta] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'payment-line-item-container payment-line-item-container-' . $delta,
          ($delta + 1) % 2 == 0 ? 'odd' : 'even',
        ),
      ),
    );
    $element['container_' . $delta]['amount'] = array(
      '#type' => 'payment_amount',
      '#title' => t('Amount'),
      '#default_value' => $line_item ? $line_item->amount : '',
      '#currency_code' => $element['#currency_code'],
      '#required' => $required,
      '#attributes' => array(
        'class' => array(
          'payment-line-item-amount',
        ),
      ),
    );
    $element['container_' . $delta]['quantity'] = array(
      '#type' => 'textfield',
      '#title' => t('Quantity'),
      '#default_value' => $line_item ? $line_item->quantity : '',
      '#size' => 3,
      '#required' => $required,
    );
    $element['container_' . $delta]['tax_rate'] = array(
      '#type' => 'textfield',
      '#title' => t('Tax rate'),
      '#default_value' => $line_item ? $line_item->tax_rate * 100 : '',
      '#size' => 5,
      '#field_suffix' => '%',
      '#required' => $required,
    );
    $element['container_' . $delta]['description'] = array(
      '#type' => 'textfield',
      '#title' => t('Description'),
      '#default_value' => $line_item ? $line_item->description : '',
      '#required' => $required,
      '#maxlength' => 255,
    );
    $element['container_' . $delta]['name'] = array(
      '#type' => 'machine_name',
      '#default_value' => $line_item ? $line_item->name : '',
      '#maxlength' => 255,
      '#machine_name' => array(
        'source' => array_merge($element['#parents'], array(
          'container_' . $delta,
          'description',
        )),
        'exists' => 'payment_method_name_exists',
      ),
      '#required' => $required,
    );
    $element['container_' . $delta]['clear'] = array(
      '#type' => 'markup',
      '#markup' => '<div class="clear"></div>',
    );
  }

  // "Add more line items" button.
  $wrapper_id = drupal_html_id('payment-ajax-replace');
  $element['add_more'] = array(
    '#type' => 'submit',
    '#value' => t('Add a line item'),
    '#submit' => array(
      'payment_form_process_line_item_submit',
    ),
    '#limit_validation_errors' => array(),
    '#ajax' => array(
      'callback' => 'payment_form_process_line_item_submit_ajax_callback',
      'effect' => 'fade',
      'event' => 'mousedown',
      'wrapper' => $wrapper_id,
    ),
    '#name' => drupal_clean_css_identifier('payment_' . $element['#name']),
    '#access' => $element['#cardinality'] == 0 || $form_state['payment_line_item_count'] < $element['#cardinality'],
    '#id' => $wrapper_id,
    '#attributes' => array(
      'class' => array(
        'payment-add-more',
      ),
    ),
  );
  return $element;
}