You are here

public function BuyXGetY::buildConfigurationForm in Commerce Core 8.2

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PromotionOfferBase::buildConfigurationForm

File

modules/promotion/src/Plugin/Commerce/PromotionOffer/BuyXGetY.php, line 130

Class

BuyXGetY
Provides the "Buy X Get Y" offer for orders.

Namespace

Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form += parent::buildConfigurationForm($form, $form_state);

  // Remove the main fieldset.
  $form['#type'] = 'container';
  $form['buy'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Customer buys'),
    '#collapsible' => FALSE,
  ];
  $form['buy']['quantity'] = [
    '#type' => 'commerce_number',
    '#title' => $this
      ->t('Quantity'),
    '#default_value' => $this->configuration['buy_quantity'],
    '#min' => 1,
  ];
  $form['buy']['conditions'] = [
    '#type' => 'commerce_conditions',
    '#title' => $this
      ->t('Matching any of the following'),
    '#parent_entity_type' => 'commerce_promotion',
    '#entity_types' => [
      'commerce_order_item',
    ],
    '#default_value' => $this->configuration['buy_conditions'],
  ];
  $form['get'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Customer gets'),
    '#collapsible' => FALSE,
  ];
  $form['get']['quantity'] = [
    '#type' => 'commerce_number',
    '#title' => $this
      ->t('Quantity'),
    '#default_value' => $this->configuration['get_quantity'],
    '#min' => 1,
  ];
  $form['get']['conditions'] = [
    '#type' => 'commerce_conditions',
    '#title' => $this
      ->t('Matching any of the following'),
    '#parent_entity_type' => 'commerce_promotion',
    '#entity_types' => [
      'commerce_order_item',
    ],
    '#default_value' => $this->configuration['get_conditions'],
  ];
  $states = $this
    ->getAutoAddStatesVisibility();
  $form['get']['auto_add_help'] = [
    '#type' => 'item',
    '#title' => $this
      ->t('Behavior'),
    '#states' => [
      'visible' => [
        $states,
      ],
    ],
  ];
  $form['get']['auto_add'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Automatically add the offer product to the cart if it isn't in it already"),
    '#description' => $this
      ->t('This behavior will only work when a single product variation (or a single product with only one variation) is specified.'),
    '#default_value' => $this->configuration['get_auto_add'],
    '#states' => [
      'visible' => [
        $states,
      ],
    ],
  ];
  $parents = array_merge($form['#parents'], [
    'offer',
    'type',
  ]);
  $selected_offer_type = NestedArray::getValue($form_state
    ->getUserInput(), $parents);
  $selected_offer_type = $selected_offer_type ?: $this->configuration['offer_type'];
  $offer_wrapper = Html::getUniqueId('buy-x-get-y-offer-wrapper');
  $form['offer'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('At a discounted value'),
    '#collapsible' => FALSE,
    '#prefix' => '<div id="' . $offer_wrapper . '">',
    '#suffix' => '</div>',
  ];
  $form['offer']['type'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Discounted by a'),
    '#title_display' => 'invisible',
    '#options' => [
      'percentage' => $this
        ->t('Percentage'),
      'fixed_amount' => $this
        ->t('Fixed amount'),
    ],
    '#default_value' => $selected_offer_type,
    '#ajax' => [
      'callback' => [
        get_called_class(),
        'ajaxRefresh',
      ],
      'wrapper' => $offer_wrapper,
    ],
  ];
  if ($selected_offer_type == 'percentage') {
    $form['offer']['percentage'] = [
      '#type' => 'commerce_number',
      '#title' => $this
        ->t('Percentage off'),
      '#default_value' => Calculator::multiply($this->configuration['offer_percentage'], '100'),
      '#maxlength' => 255,
      '#min' => 0,
      '#max' => 100,
      '#size' => 4,
      '#field_suffix' => $this
        ->t('%'),
      '#required' => TRUE,
    ];
  }
  else {
    $form['offer']['amount'] = [
      '#type' => 'commerce_price',
      '#title' => $this
        ->t('Amount off'),
      '#default_value' => $this->configuration['offer_amount'],
      '#required' => TRUE,
    ];
  }
  $form['limit'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Offer limit'),
    '#description' => $this
      ->t('The number of times this offer can apply to the same order.'),
    '#collapsible' => FALSE,
  ];
  $form['limit']['amount'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('Limited to'),
    '#title_display' => 'invisible',
    '#options' => [
      0 => $this
        ->t('Unlimited'),
      1 => $this
        ->t('Limited number of uses'),
    ],
    '#default_value' => $this->configuration['offer_limit'] ? 1 : 0,
  ];
  $form['limit']['offer_limit'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Number of uses'),
    '#title_display' => 'invisible',
    '#default_value' => $this->configuration['offer_limit'] ?: 1,
    '#states' => [
      'invisible' => [
        ':input[name="offer[0][target_plugin_configuration][order_buy_x_get_y][limit][amount]"]' => [
          'value' => 0,
        ],
      ],
    ],
  ];
  return $form;
}