You are here

function commerce_line_item_form_alter in Commerce Core 7

Implements hook_form_alter().

Alter the views form with functionality specific to line items. This form currently only supports line items from a single order, and it determines which order the line items are for based on a Views argument.

File

modules/line_item/commerce_line_item.module, line 106
Defines the core Commerce line item entity and API functions interact with line items on orders.

Code

function commerce_line_item_form_alter(&$form, &$form_state, $form_id) {
  $line_item_form = FALSE;

  // Is this a views form?
  if (strpos($form_id, 'views_form_') === 0) {
    $view = $form_state['build_info']['args'][0];

    // Does the view contain one of the line item edit fields?
    foreach ($view->field as $field_name => $field) {
      if ($field instanceof commerce_line_item_handler_field_edit_delete || $field instanceof commerce_line_item_handler_field_edit_quantity) {
        $line_item_form = TRUE;
      }
    }
  }

  // This is not the form we are looking for.
  if (!$line_item_form) {
    return;
  }

  // Require the existence of an order_id argument (and its value).
  if (empty($view->argument['order_id']) || empty($view->argument['order_id']->value)) {
    return;
  }
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_line_item') . '/theme/commerce_line_item.theme.css';
  $form['#attached']['js'][] = drupal_get_path('module', 'commerce_line_item') . '/commerce_line_item.js';
  $form['#submit'][] = 'commerce_line_item_line_item_views_form_submit';

  // Wrap the form in a div so we can add CSS and javascript behaviors to it.
  $form['#prefix'] = '<div class="commerce-line-item-views-form">';
  $form['#suffix'] = '</div>';

  // Add an additional class to the actions wrapper.
  $form['actions']['#attributes']['class'][] = 'commerce-line-item-actions';

  // Load the order from the Views argument.
  $order = commerce_order_load($view->argument['order_id']->value[0]);
  $form_state['order'] = $order;
}