You are here

public function CombinationOffer::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/CombinationOffer.php, line 65

Class

CombinationOffer
Provides the 'combination_offer' offer plugin.

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';
  $wrapper_id = Html::getUniqueId('combination-offer-ajax-wrapper');
  $form['#prefix'] = '<div id="' . $wrapper_id . '">';
  $form['#suffix'] = '</div>';

  // Remove the combination offer from the allowed plugins.
  $definitions = array_diff_key($this->offerManager
    ->getDefinitions(), [
    $this->pluginId => '',
  ]);
  $plugins = array_map(static function ($definition) {
    return $definition['label'];
  }, $definitions);
  asort($plugins);
  $user_input = (array) NestedArray::getValue($form_state
    ->getUserInput(), $form['#parents']);

  // Initialize the offers form.
  if (!$form_state
    ->get('offers_form_initialized')) {
    $offers = $this->configuration['offers'] ?: [
      NULL,
    ];

    // Initialize the offers with the user input if present.
    if (isset($user_input['offers'])) {
      $offers = $user_input['offers'];
    }
    $form_state
      ->set('offers', $offers);
    $form_state
      ->set('offers_form_initialized', TRUE);
  }
  $class = get_class($this);
  foreach ($form_state
    ->get('offers') as $index => $offer) {

    // Override the offer with the user input if present.
    if (!empty($user_input['offers'][$index])) {
      $offer = $user_input['offers'][$index];
    }
    $offer_form =& $form['offers'][$index];
    $offer_form['configuration'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Offer #@index', [
        '@index' => $index + 1,
      ]),
      '#tree' => FALSE,
    ];
    $offer_form['configuration']['target_plugin_id'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Type'),
      '#options' => $plugins,
      '#default_value' => $offer['target_plugin_id'] ?? '',
      // Force the user to select a value.
      '#empty_value' => '',
      // Only require at least an offer.
      '#required' => $index === 0,
      '#ajax' => [
        'callback' => [
          $class,
          'ajaxCallback',
        ],
        'wrapper' => $wrapper_id,
      ],
      '#parents' => array_merge($form['#parents'], [
        'offers',
        $index,
        'target_plugin_id',
      ]),
    ];

    // When a target plugin ID is selected, embed the offer configuration
    // form.
    if (!empty($offer['target_plugin_id'])) {
      $inline_form = $this->inlineFormManager
        ->createInstance('plugin_configuration', [
        'plugin_type' => 'commerce_promotion_offer',
        'plugin_id' => $offer['target_plugin_id'],
        'plugin_configuration' => $offer['target_plugin_configuration'] ?? [],
      ]);
      $offer_form['configuration']['target_plugin_configuration'] = [
        '#inline_form' => $inline_form,
        '#parents' => array_merge($form['#parents'], [
          'offers',
          $index,
          'target_plugin_configuration',
        ]),
      ];
      $offer_form['configuration']['target_plugin_configuration'] = $inline_form
        ->buildInlineForm($offer_form['configuration']['target_plugin_configuration'], $form_state);
    }

    // Don't allow removing the first offer.
    if ($index > 0) {
      $offer_form['configuration']['remove'] = [
        '#type' => 'submit',
        '#name' => 'remove_offer' . $index,
        '#value' => $this
          ->t('Remove'),
        '#limit_validation_errors' => [],
        '#submit' => [
          [
            $class,
            'removeOfferSubmit',
          ],
        ],
        '#offer_index' => $index,
        '#parents' => array_merge($form['#parents'], [
          'offers',
          $index,
          'remove',
        ]),
        '#ajax' => [
          'callback' => [
            $class,
            'ajaxCallback',
          ],
          'wrapper' => $wrapper_id,
        ],
      ];
    }
  }
  $form['offers'][] = [
    'add_offer' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add another offer'),
      '#submit' => [
        [
          $class,
          'addOfferSubmit',
        ],
      ],
      '#limit_validation_errors' => [
        array_merge($form['#parents'], [
          'offers',
        ]),
      ],
      '#ajax' => [
        'callback' => [
          $class,
          'ajaxCallback',
        ],
        'wrapper' => $wrapper_id,
      ],
    ],
  ];
  return $form;
}