You are here

function commerce_discount_form in Commerce Discount 7

Form callback: create or edit a discount.

Parameters

array $form: Standard Drupal $form.

array $form_state: Standard Drupal $form_state.

CommerceDiscount $commerce_discount: Commerce discount object.

string $op: Current operation.

Return value

array A renderable form build array.

File

includes/commerce_discount.admin.inc, line 137
Commerce discount editing UI.

Code

function commerce_discount_form($form, &$form_state, CommerceDiscount $commerce_discount, $op = 'edit') {

  // We might have gotten the commerce discount type via ajax, so set it
  // in the commerce discount entity.
  if (!empty($form_state['values']['commerce_discount_type'])) {
    $commerce_discount->type = $form_state['values']['commerce_discount_type'];
  }
  $options = array();
  foreach (commerce_discount_types() as $type => $info) {
    $options[$type] = $info['label'];
  }
  $form['label'] = array(
    '#title' => t('Admin title'),
    '#type' => 'textfield',
    '#default_value' => $commerce_discount->label,
    '#description' => t('Shown only on management screens, not shown to customers.'),
    '#required' => TRUE,
  );
  $form['component_title'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#default_value' => empty($commerce_discount->component_title) ? t('Discount') : $commerce_discount->component_title,
    '#description' => t('Shown to customers. Defaults to "Discount".'),
  );

  // Machine-readable type name.
  $form['name'] = array(
    '#type' => 'machine_name',
    // Strip the 'discount_' prefix from the beginning of the string.
    '#default_value' => isset($commerce_discount->name) ? substr($commerce_discount->name, 9) : '',
    '#disabled' => $commerce_discount
      ->hasStatus(ENTITY_IN_CODE),
    '#machine_name' => array(
      'exists' => '_commerce_discount_name_exists',
      'source' => array(
        'label',
      ),
    ),
    '#description' => t('A unique machine-readable name for this message type. It must only contain lowercase letters, numbers, and underscores.'),
    // 64 characters minus the 9 character 'discount_' prefix.
    '#maxlength' => 64 - 9,
    // This field should stay LTR even for RTL languages.
    '#field_prefix' => '<span dir="ltr">discount_',
    '#field_suffix' => '</span>&lrm;',
  );

  // Show a list of commerce discounts.
  $form['commerce_discount_type'] = array(
    '#title' => t('Choose discount type'),
    '#type' => 'radios',
    '#options' => $options,
    '#required' => FALSE,
    '#default_value' => $commerce_discount->type,
    '#ajax' => array(
      'callback' => 'commerce_discount_fields_ajax_callback',
      'wrapper' => 'commerce-discount-fields-wrapper',
    ),
  );
  $form['commerce_discount_fields'] = array(
    '#prefix' => '<div id="commerce-discount-fields-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
    '#parents' => array(
      'commerce_discount_fields',
    ),
    '#pre_render' => array(
      'commerce_discount_form_pre_render',
    ),
  );

  // Vertical tabs container.
  $form['commerce_discount_fields']['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
    'discount_options' => array(
      '#type' => 'fieldset',
      '#title' => t('Discount options'),
      '#collapsible' => TRUE,
      '#weight' => -10,
    ),
    'commerce_discount_compatibility' => array(
      '#type' => 'fieldset',
      '#title' => t('Discount compatibility'),
      '#collapsible' => TRUE,
    ),
  );

  // Add radios for enabling or disabling this discount.
  $form['commerce_discount_fields']['additional_settings']['discount_options']['status'] = array(
    '#title' => t('Discount status'),
    '#type' => 'radios',
    '#parents' => array(
      'status',
    ),
    '#options' => array(
      TRUE => t('Active'),
      FALSE => t('Disabled'),
    ),
    '#required' => FALSE,
    '#default_value' => $commerce_discount->status,
  );

  // Add the sort order select list.
  $form['commerce_discount_fields']['additional_settings']['discount_options']['sort_order'] = array(
    '#type' => 'select',
    '#parents' => array(
      'sort_order',
    ),
    '#title' => t('Sort order'),
    '#description' => t('Discounts will be sorted by this value to be evaluated in that order.'),
    '#options' => drupal_map_assoc(range(1, 21)),
    '#default_value' => !empty($commerce_discount->sort_order) ? $commerce_discount->sort_order : 10,
  );
  field_attach_form('commerce_discount', $commerce_discount, $form['commerce_discount_fields'], $form_state);

  // Remove the title and surrounding fieldset from the offer reference field.
  $form['commerce_discount_fields']['commerce_discount_offer'][LANGUAGE_NONE]['#title'] = NULL;
  $form['commerce_discount_fields']['commerce_discount_offer'][LANGUAGE_NONE]['#type'] = 'container';

  // Add a class to custom fields for easier styling.
  // Ignore the offer reference field and any date fields. Date fields are
  // ignored because their markup makes them hard to style generically.
  $date_field_types = array(
    'date',
    'datestamp',
    'datetime',
  );
  foreach (element_get_visible_children($form['commerce_discount_fields']) as $field_name) {
    $field = field_info_field($field_name);
    if ($field_name == 'commerce_discount_offer' || !isset($field['type']) || in_array($field['type'], $date_field_types)) {
      continue;
    }
    $field_form =& $form['commerce_discount_fields'][$field_name];
    if (!isset($field_form['#attributes']['class'])) {
      $field_form['#attributes']['class'] = array();
    }
    $field_form['#attributes']['class'][] = 'commerce-discount-box';
  }

  // Give the compatibility strategy field a title and a description linking to
  // the compatibility documentation based on the discount type.
  switch ($form['commerce_discount_fields']['commerce_compatibility_strategy'][LANGUAGE_NONE]['#bundle']) {
    case 'product_discount':
      $discount_type_name = t('Product discounts');
      $description = t('This setting determines which product discounts this discount can be applied after or that can be applied to the same product line item after this one.');
      break;
    case 'order_discount':
      $discount_type_name = t('Order discounts');
      $description = t('This setting determines which order discounts this discount can be applied after or that can be applied to the same order after this one.');
      break;
    default:
      $discount_type_name = t('discounts of the same type');
      $description = t('This setting determines which discounts of the same type this discount can be applied after or that can be applied to the same item after this one.');
      break;
  }
  $title = t('Compatibility with other %name', array(
    '%name' => $discount_type_name,
  ));
  $form['commerce_discount_fields']['commerce_compatibility_strategy'][LANGUAGE_NONE]['#title'] = $title;
  $form['commerce_discount_fields']['commerce_compatibility_strategy'][LANGUAGE_NONE]['#description'] = $description . '<br />' . t('For more information, refer to the discount compatibility !link on drupal.org.', array(
    '!link' => '<a href="https://www.drupal.org/node/2917774" target="_blank">' . t('documentation page') . '</a>',
  ));

  // Ensure the compatibility strategy field has a default value.
  if (empty($form['commerce_discount_fields']['commerce_compatibility_strategy'][LANGUAGE_NONE]['#default_value'])) {
    $form['commerce_discount_fields']['commerce_compatibility_strategy'][LANGUAGE_NONE]['#default_value'] = 'any';
  }

  // Add visibility states to the discount compatibility selection field.
  $form['commerce_discount_fields']['commerce_compatibility_selection']['#states'] = array(
    'visible' => array(
      ':input[name="commerce_discount_fields[commerce_compatibility_strategy][' . LANGUAGE_NONE . ']"]' => array(
        array(
          'value' => 'except',
        ),
        array(
          'value' => 'only',
        ),
      ),
    ),
  );

  // We have to do some extra adjustments to move compatibility fields into
  // the vertical tabs.
  $form['commerce_discount_fields']['additional_settings']['commerce_discount_compatibility']['commerce_compatibility_strategy'] = $form['commerce_discount_fields']['commerce_compatibility_strategy'];
  $form['commerce_discount_fields']['additional_settings']['commerce_discount_compatibility']['commerce_compatibility_strategy']['#parents'] = array(
    'commerce_discount_fields',
    'commerce_compatibility_strategy',
  );
  $form['commerce_discount_fields']['additional_settings']['commerce_discount_compatibility']['commerce_compatibility_selection'] = $form['commerce_discount_fields']['commerce_compatibility_selection'];
  $form['commerce_discount_fields']['additional_settings']['commerce_discount_compatibility']['commerce_compatibility_selection']['#parents'] = array(
    'commerce_discount_fields',
    'commerce_compatibility_selection',
  );
  unset($form['commerce_discount_fields']['commerce_compatibility_strategy'], $form['commerce_discount_fields']['commerce_compatibility_selection']);
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save discount'),
    '#weight' => 40,
    '#ief_submit_all' => TRUE,
    '#submit' => NULL,
  );
  if (!$commerce_discount
    ->hasStatus(ENTITY_IN_CODE) && $op != 'add') {
    $form['actions']['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete discount'),
      '#weight' => 45,
      '#limit_validation_errors' => array(),
      '#submit' => array(
        'commerce_discount_form_submit_delete',
      ),
      '#suffix' => l(t('Cancel'), 'admin/commerce/discounts'),
    );
  }
  else {
    $form['actions']['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/discounts');
  }
  $form['commerce_discount_fields']['additional_settings']['commerce_discount_usage'] = array(
    '#type' => 'fieldset',
    '#title' => t('Maximum usage'),
    '#collapsible' => TRUE,
  );
  $form['commerce_discount_fields']['additional_settings']['discount_date'] = array(
    '#type' => 'fieldset',
    '#title' => t('Discount dates'),
    '#collapsible' => TRUE,
    '#weight' => -1,
  );

  // Add assets.
  $form['#attached']['js'][] = drupal_get_path('module', 'commerce_discount') . '/js/commerce_discount.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'commerce_discount') . '/css/commerce_discount.css';
  $form['#attributes']['class'][] = 'commerce-discount-form';
  return $form;
}