You are here

function commerce_bpc_create_bulk_form in Commerce Bulk Product Creation 7.2

Same name and namespace in other branches
  1. 7 commerce_bpc.forms.inc \commerce_bpc_create_bulk_form()

Form constructor for the bulk creation form.

Paths: admin/commerce/products/add-bulk/PRODUCT_TYPE

See also

commerce_bpc_menu()

commerce_bpc_create_bulk_form_validate()

commerce_bpc_create_bulk_form_submit()

1 string reference to 'commerce_bpc_create_bulk_form'
commerce_bpc_menu in ./commerce_bpc.module
Implements hook_menu().

File

./commerce_bpc.forms.inc, line 17
Form generation functions for the Commerce bulk product creation module

Code

function commerce_bpc_create_bulk_form($form, &$form_state, $product_type) {
  $form['#parents'] = array();

  // Create a temporary product object attach fields.
  $new_product = commerce_product_new($product_type);
  $language = !empty($new_product->language) ? $new_product->language : LANGUAGE_NONE;

  // Store the product type for later use.
  $form['product_type'] = array(
    '#type' => 'value',
    '#value' => $product_type,
  );
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('Bulk Product Creation Form'),
  );
  $form['product'] = array(
    '#type' => 'fieldset',
    '#title' => t('Product Info'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['combinations'] = array(
    '#type' => 'fieldset',
    '#title' => t('Combinations'),
    '#description' => t('A product will be created for every possible combination of the values you select here.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $form['static_values'] = array(
    '#type' => 'fieldset',
    '#title' => t('Static values'),
    '#description' => t('The values of these fields will be shared by all generated products.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
    '#parents' => array(
      'static_values',
    ),
  );
  $form['product']['sku_fragment'] = array(
    '#type' => 'textfield',
    '#title' => t('SKU'),
    '#required' => TRUE,
    '#description' => t("The part of the SKU that is common to all products to be generated. The SKU of each individual product will be composed of this value together with values of the selected options."),
    '#size' => 60,
    '#maxlength' => 255,
    '#weight' => 0,
    '#process' => array(
      'commerce_bpc_process_fragment_field',
    ),
  );
  $form['product']['title_fragment'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#required' => TRUE,
    '#description' => t("The part of the product title that is common to all products to be generated. The SKU of each individual product will be composed of this value together with values of the selected options."),
    '#size' => 60,
    '#maxlength' => 255,
    '#process' => array(
      'commerce_bpc_process_fragment_field',
    ),
  );

  // We simply attach all fields to the 'static_values' fieldset, and then
  // allow modules to act on each of the fields.
  field_attach_form('commerce_product', $new_product, $form['static_values'], $form_state, $language);
  commerce_bpc_process_field_form_elements($form, $form_state);
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create products'),
    '#submit' => array(
      'commerce_bpc_create_bulk_form_submit',
    ),
  );

  // If translation support is enabled, provide the suitable languages
  if (module_exists('entity_translation') && entity_translation_enabled('commerce_product')) {
    $handler = entity_translation_get_handler('commerce_product', $new_product);
    $translations = $handler
      ->getTranslations();
    $form['static_values']['language'] = array(
      '#type' => 'select',
      '#title' => t('Language'),
      '#default_value' => isset($new_product->language) ? $new_product->language : '',
      '#options' => array(
        LANGUAGE_NONE => t('Language neutral'),
      ) + locale_language_list('name'),
      '#weight' => 190,
    );

    // Since this function may change the language of the submitted form values,
    // it has to be the first called.
    array_unshift($form['actions']['submit']['#submit'], 'commerce_bpc_create_bulk_form_translation_submit');
  }
  $form['static_values']['status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#description' => t('Disabled products cannot be added to shopping carts and may be hidden in administrative product lists.'),
    '#options' => array(
      '1' => t('Active'),
      '0' => t('Disabled'),
    ),
    '#default_value' => 1,
    '#required' => TRUE,
    '#weight' => 200,
  );
  return $form;
}