You are here

public function ShipmentWeightMultipleConds::buildConfigurationForm in Commerce Shipping Weight Tariff 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Commerce/Condition/ShipmentWeightMultipleConds.php \Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\Condition\ShipmentWeightMultipleConds::buildConfigurationForm()

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 ConditionBase::buildConfigurationForm

File

src/Plugin/Commerce/Condition/ShipmentWeightMultipleConds.php, line 129

Class

ShipmentWeightMultipleConds
Provides the multiple weight condition for shipments.

Namespace

Drupal\commerce_shipping_weight_tariff\Plugin\Commerce\Condition

Code

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

  // Preparing condition fieldsets.
  for ($i = 1; $i <= 10; $i++) {
    $weight = $this->configuration['condition_' . $i]['weight'];
    $price = $this->configuration['condition_' . $i]['price'];
    $enabled = $this->configuration['condition_' . $i]['enabled'];
    $operator = $this->configuration['condition_' . $i]['operator'];
    $form['condition_' . $i] = [
      '#type' => 'fieldset',
      '#title' => 'Condition group #' . $i,
    ];
    $form['condition_' . $i]['enabled'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enabled?'),
      '#default_value' => $enabled,
    ];

    // Check 'Enabled' checkbox if condition group should be enabled.
    if ($enabled) {
      $form['condition_' . $i]['enabled']['#attributes'] = [
        'checked' => 'checked',
      ];
    }

    // Show condition group fields only when 'Enabled' checkbox is checked.
    $form['condition_' . $i]['price'] = [
      '#type' => 'commerce_price',
      '#title' => t('Price'),
      '#default_value' => $price,
      '#required' => FALSE,
      '#states' => [
        'invisible' => [
          ':input[name="conditions[form][shipment][shipment_weight_multiple_conds][configuration][form][condition_' . $i . '][enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
        'required' => [
          ':input[name="conditions[form][shipment][shipment_weight_multiple_conds][configuration][form][condition_' . $i . '][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['condition_' . $i]['operator'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Operator'),
      '#options' => $this
        ->getComparisonOperators(),
      '#default_value' => $operator,
      '#required' => FALSE,
      '#states' => [
        'invisible' => [
          ':input[name="conditions[form][shipment][shipment_weight_multiple_conds][configuration][form][condition_' . $i . '][enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
        'required' => [
          ':input[name="conditions[form][shipment][shipment_weight_multiple_conds][configuration][form][condition_' . $i . '][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['condition_' . $i]['weight'] = [
      '#type' => 'physical_measurement',
      '#measurement_type' => MeasurementType::WEIGHT,
      '#title' => $this
        ->t('Weight'),
      '#default_value' => $weight,
      '#required' => FALSE,
      '#states' => [
        'invisible' => [
          ':input[name="conditions[form][shipment][shipment_weight_multiple_conds][configuration][form][condition_' . $i . '][enabled]"]' => [
            'checked' => FALSE,
          ],
        ],
        'required' => [
          ':input[name="conditions[form][shipment][shipment_weight_multiple_conds][configuration][form][condition_' . $i . '][enabled]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
  }
  return $form;
}