You are here

function commerce_line_item_field_widget_form in Commerce Core 7

Implements hook_field_widget_form().

Used to define the form element for custom widgets.

File

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

Code

function commerce_line_item_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Define the complex line item reference field widget.
  if ($instance['widget']['type'] == 'commerce_line_item_manager') {
    $line_item_ids = array();

    // Build an array of line item IDs from this field's values.
    foreach ($items as $item) {
      $line_item_ids[] = $item['line_item_id'];
    }

    // Load the line items for temporary storage in the form array.
    if (!empty($line_item_ids)) {
      $line_items = commerce_line_item_load_multiple($line_item_ids);
    }
    else {
      $line_items = array();
    }

    // Update the base form element array to use the proper theme and validate
    // functions and to include header information for the line item table.
    $element += array(
      '#theme' => 'commerce_line_item_manager',
      '#element_validate' => array(
        'commerce_line_item_manager_validate',
      ),
      '#header' => array(
        t('Remove'),
        t('Title'),
        t('SKU'),
        t('Unit price'),
        t('Quantity'),
        t('Total'),
      ),
      '#empty' => t('No line items found.'),
      'line_items' => array(),
    );
    if (!empty($form_state['line_item_save_warning'])) {
      drupal_set_message(t('New line items on this order will not be saved until the <em>Save order</em> button is clicked.'), 'warning');

      // Set variable to false to prevent it from showing up in other contexts.
      $form_state['line_item_save_warning'] = FALSE;
    }

    // Add a set of elements to the form for each referenced line item.
    foreach ($line_items as $line_item_id => $line_item) {

      // Store the original line item for later comparison.
      $element['line_items'][$line_item_id]['line_item'] = array(
        '#type' => 'value',
        '#value' => $line_item,
      );

      // This checkbox will be overridden with a clickable delete image.
      $element['line_items'][$line_item_id]['remove'] = array(
        '#type' => 'checkbox',
        '#default_value' => FALSE,
      );
      $element['line_items'][$line_item_id]['title'] = array(
        '#markup' => commerce_line_item_title($line_item),
      );
      $element['line_items'][$line_item_id]['line_item_label'] = array(
        '#markup' => check_plain($line_item->line_item_label),
      );

      // Retrieve the widget form for just the unit price.
      $widget_form = _field_invoke_default('form', 'commerce_line_item', $line_item, $form, $form_state, array(
        'field_name' => 'commerce_unit_price',
      ));

      // Unset the title and description and add it to the line item form.
      $language = $widget_form['commerce_unit_price']['#language'];
      $widget_form['commerce_unit_price'][$language][0]['amount']['#title_display'] = 'invisible';
      $element['line_items'][$line_item_id]['commerce_unit_price'] = $widget_form['commerce_unit_price'];
      $quantity = round($line_item->quantity);
      $element['line_items'][$line_item_id]['quantity'] = array(
        '#type' => 'textfield',
        '#datatype' => 'integer',
        '#default_value' => $quantity,
        '#size' => 4,
        '#maxlength' => max(4, strlen($quantity)),
      );

      // Wrap the line item and add its formatted total to the form.
      $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
      $element['line_items'][$line_item_id]['commerce_total'] = array(
        '#markup' => commerce_currency_format($wrapper->commerce_total->amount
          ->value(), $wrapper->commerce_total->currency_code
          ->value(), $line_item),
      );
    }

    // If the the form has been instructed to add a line item...
    if (!empty($form_state['line_item_add'])) {

      // Load the info object for the selected line item type.
      $line_item_type = commerce_line_item_type_load($form_state['line_item_add']);

      // Store the line item info object in the form array.
      $element['actions']['line_item_type'] = array(
        '#type' => 'value',
        '#value' => $line_item_type,
      );

      // If this type specifies a valid add form callback function...
      if ($callback = commerce_line_item_type_callback($line_item_type, 'add_form')) {

        // Load in the appropriate form elements to the actions array.
        $element['actions'] += $callback($element, $form_state);
      }

      // Add a default save button.
      $element['actions'] += array(
        'save_line_item' => array(
          '#type' => 'button',
          '#value' => !empty($line_item_type['add_form_submit_value']) ? $line_item_type['add_form_submit_value'] : t('Save'),
          '#limit_validation_errors' => array(
            array_merge($element['#field_parents'], array(
              $field['field_name'],
            )),
          ),
          '#ajax' => array(
            'callback' => 'commerce_line_item_manager_refresh',
            'wrapper' => 'line-item-manager',
          ),
        ),
      );
      $element['actions']['cancel'] = array(
        '#type' => 'button',
        '#value' => t('Cancel'),
        '#limit_validation_errors' => array(),
        '#ajax' => array(
          'callback' => 'commerce_line_item_manager_refresh',
          'wrapper' => 'line-item-manager',
        ),
      );
    }
    else {

      // Otherwise display the select list to add a new line item.
      $options = commerce_line_item_type_get_name();

      // Only display the line item selector if line item types exist.
      if (!empty($options)) {
        $element['actions']['line_item_type'] = array(
          '#type' => 'select',
          '#options' => commerce_line_item_type_get_name(),
          '#prefix' => '<div class="add-line-item">',
        );
        $element['actions']['line_item_add'] = array(
          '#type' => 'button',
          '#value' => t('Add line item'),
          '#limit_validation_errors' => array(
            array_merge($element['#field_parents'], array(
              $field['field_name'],
            )),
          ),
          '#ajax' => array(
            'callback' => 'commerce_line_item_manager_refresh',
            'wrapper' => 'line-item-manager',
          ),
          '#suffix' => '</div>',
        );
      }
    }
    return $element;
  }
  elseif ($instance['widget']['type'] == 'commerce_line_item_reference_hidden') {
    return array();
  }
}