You are here

function _webform_edit_productfield in Commerce Webform 7.2

Same name and namespace in other branches
  1. 8 productfield.inc \_webform_edit_productfield()
  2. 7 productfield.inc \_webform_edit_productfield()

Implements _webform_edit_component().

File

./productfield.inc, line 43

Code

function _webform_edit_productfield($component) {
  $form = array();
  $items = isset($component['extra']['items']) && is_array($component['extra']['items']) ? $component['extra']['items'] : array();
  $product_ids = array();
  $skus = array();

  // Build an array of product IDs from this field's values.
  foreach ($items as $item) {
    $product_ids[] = $item['product_id'];
    $skus[] = $item['sku'];
  }
  $form['extra']['product_type'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#options' => commerce_product_type_options_list(),
    '#title' => t('Product type'),
    '#default_value' => $component['extra']['product_type'],
    '#description' => t('Use either this OR the product skus field below. By selecting a product type here, all products of this type will be available for selection on the webform.'),
    '#weight' => 1,
  );
  $form['extra']['items'] = array(
    '#type' => 'textfield',
    '#title' => t('Product skus'),
    '#description' => t('Use this instead of setting a product type above. List the product skus you would like to offer as options on this webform. When the webform is saved, the user has that product added to thier basket.'),
    '#default_value' => implode(', ', $skus),
    '#autocomplete_path' => 'commerce_webform/autocomplete',
    '#size' => 128,
    '#maxlength' => 2048,
    '#element_validate' => array(
      '_webform_edit_validate_productfield',
    ),
    '#weight' => 2,
  );
  $form['value'] = array(
    '#type' => 'textfield',
    '#title' => t('Default sku'),
    '#default_value' => $component['value'],
    '#description' => t('Enter the product sku which should be selected by default or leave blank for no default. A token can be used here, e.g. [current-page:query:sku] to retrieve it from the URL query parameter sku.'),
    '#size' => 60,
    '#maxlength' => 1024,
    '#weight' => 3,
  );
  $form['extra']['multiple'] = array(
    '#type' => 'checkbox',
    '#title' => t('Multiple'),
    '#default_value' => $component['extra']['multiple'],
    '#description' => t('Check this option if the user should be allowed to choose multiple values.'),
    '#weight' => 4,
  );
  $form['extra']['choose_quantity'] = array(
    '#type' => 'checkbox',
    '#title' => t('Allow the user to set the quantity.'),
    '#default_value' => empty($component['extra']['choose_quantity']) ? 0 : $component['extra']['choose_quantity'],
    '#description' => t('Check this option if the user should be allowed to set the number of products selected. The default is 1 for single selections and 0 if it is possbile to select multiple products.'),
    '#weight' => 5,
  );
  $form['extra']['choose_quantity_min'] = array(
    '#type' => 'textfield',
    '#size' => '6',
    '#title' => t('Minimum quantity:'),
    '#states' => array(
      'invisible' => array(
        ':input[name="extra[choose_quantity]"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
    '#default_value' => empty($component['extra']['choose_quantity_min']) ? 0 : $component['extra']['choose_quantity_min'],
    '#description' => t('Minimum quantity the user can select.'),
    '#weight' => 6,
  );
  $form['extra']['choose_quantity_max'] = array(
    '#type' => 'textfield',
    '#size' => '6',
    '#title' => t('Maximum quantity:'),
    '#states' => array(
      'invisible' => array(
        ':input[name="extra[choose_quantity]"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
    '#default_value' => empty($component['extra']['choose_quantity_max']) ? 0 : $component['extra']['choose_quantity_max'],
    '#description' => t('Maximum quantity the user can select. Leave at 0 if you would like to render a textfield instead of a select option.'),
    '#weight' => 7,
  );
  $form['display']['aslist'] = array(
    '#type' => 'checkbox',
    '#title' => t('Listbox'),
    '#default_value' => $component['extra']['aslist'],
    '#description' => t('Check this option if you want the select component to be of list box type instead of radio buttons or checkboxes. This does nothing if both multiple and choose quantity are selected above.'),
    '#weight' => 8,
    '#parents' => array(
      'extra',
      'aslist',
    ),
  );
  $form['extra']['hide_price'] = array(
    '#type' => 'select',
    '#title' => t('Hide prices'),
    '#options' => array(
      'show' => t('Show all prices (default)'),
      'hide' => t('Hide all prices'),
      'zero' => t('Hide zero prices'),
    ),
    '#default_value' => empty($component['extra']['hide_price']) ? 'show' : $component['extra']['hide_price'],
    '#description' => t('Check this option if product prices should be hidden in webform for none, all or only zero priced products. This has no effect on other product displays like shopping cart and checkout.'),
    '#weight' => 8,
  );
  return $form;
}