You are here

function commerce_option_form in Commerce Product Option 7.2

Commerce Option create/edit form

1 string reference to 'commerce_option_form'
commerce_option_get_option_set_fields in ./commerce_option.admin.inc
Ajax callback that replaces the fields-part of the form with the fields defined in the Commerce Option Set (type).

File

./commerce_option.admin.inc, line 6

Code

function commerce_option_form($form, &$form_state, $option, $op = 'edit') {

  // Disable adding new options for now, until we figure out how to properly
  // attach it to an order.
  if ($op == 'add') {
    return array(
      'info' => array(
        '#markup' => t('Adding an option through the UI is currently not supported.'),
      ),
    );
  }
  $form = array(
    '#parents' => array(),
    'data' => array(
      '#tree' => TRUE,
    ),
  );
  if ($op != 'add') {

    // Create extra info that is not editable
    $line_item = commerce_line_item_load($option->line_item_id);
    $liw = entity_metadata_wrapper('commerce_line_item', $line_item);
    $product_title = $liw->commerce_product->title
      ->value();
    $order_id = $liw->order_id
      ->value();
    $order_id = l($order_id, 'admin/commerce/orders/' . $order_id . '/view');
    $form['info'] = array(
      '#type' => 'item',
      '#title' => t('Order info'),
      '#markup' => t('This option is attached to product %product in order !order', array(
        '%product' => $product_title,
        '!order' => $order_id,
      )),
    );
  }
  $option_sets_raw = commerce_option_set_load_multiple(FALSE);
  $option_sets = array();
  foreach ($option_sets_raw as $set) {
    $option_sets[$set->set_id] = $set->name;
  }
  $form['type'] = array(
    '#type' => 'select',
    '#title' => t('Type'),
    '#options' => $option_sets,
    '#disabled' => $op == 'add' ? FALSE : TRUE,
    '#ajax' => array(
      'callback' => 'commerce_option_get_option_set_fields',
    ),
  );

  // Set the fields attached to the option bundle.
  $form['fields'] = commerce_option_build_option_set_fields($form, $form_state, $op, $option);
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Save options'),
      '#weight' => 40,
    ),
  );
  if ($op != 'add') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete options'),
      '#weight' => 45,
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'commerce_option_form_submit_delete',
      ),
    );
  }
  return $form;
}