You are here

function commerce_coupon_attach_coupon_entity_form in Commerce Coupon 7.2

Attach a coupon entity form to a form or form fragment.

Parameters

array $form: Drupal form array.

array $form_state: Drupal formstate array.

object $coupon: Coupon entity.

bool $show_discounts_field: Whether or not to show the discount reference field.

2 calls to commerce_coupon_attach_coupon_entity_form()
commerce_coupon_attach_ajax_coupon_entity_form in ./commerce_coupon.module
Attach an ajax coupon entity form to a form or form fragment.
commerce_coupon_form in includes/commerce_coupon.admin.inc
Form callback: coupon add/edit form.

File

./commerce_coupon.module, line 1961
Provides coupon functionality for Drupal Commerce.

Code

function commerce_coupon_attach_coupon_entity_form(array &$form, array &$form_state, $coupon, $show_discounts_field = FALSE) {
  global $user;

  // Coupon code.
  $form['code'] = array(
    '#type' => 'textfield',
    '#title' => t('Coupon code'),
    '#default_value' => $coupon->code,
    '#coupon_id' => isset($coupon->coupon_id) ? $coupon->coupon_id : '',
    '#element_validate' => array(
      'commerce_coupon_validate_code',
    ),
    '#weight' => -10,
  );

  // Generate code.
  $form['generate'] = array(
    '#type' => 'checkbox',
    '#title' => t('Generate code'),
    '#description' => t('If you check this, a random code will be generated.'),
    '#default_value' => FALSE,
    '#weight' => -8,
  );

  // Set user id.
  $uid = !empty($coupon->is_new) ? $user->uid : $coupon->uid;
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $uid,
    '#weight' => -7,
  );
  $coupon->uid = $uid;
  $field_parents = !empty($form['#parents']) ? $form['#parents'] : array();
  $field_parents[] = 'commerce_coupon_fields';
  $form['commerce_coupon_fields'] = array(
    '#type' => 'container',
    '#parents' => $field_parents,
    '#weight' => -6,
  );

  // Attach fields.
  field_attach_form('commerce_coupon', $coupon, $form['commerce_coupon_fields'], $form_state);

  // Possibly do not show the discount reference field.
  if (!$show_discounts_field) {
    unset($form['commerce_coupon_fields']['commerce_discount_reference']);
  }

  // Status.
  $form['status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enabled'),
    '#default_value' => $coupon->status,
    '#weight' => -6,
  );

  // Allow other modules to alter this form whereever it appears.
  drupal_alter('commerce_coupon_coupon_entity_form', $form, $form_state, $coupon);
}