You are here

public function Custom::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 TaxTypeBase::buildConfigurationForm

File

modules/tax/src/Plugin/Commerce/TaxType/Custom.php, line 104

Class

Custom
Provides the Custom tax type.

Namespace

Drupal\commerce_tax\Plugin\Commerce\TaxType

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);
  $form['display_label'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Display label'),
    '#description' => $this
      ->t('Used to identify the applied tax in order summaries.'),
    '#options' => $this
      ->getDisplayLabels(),
    '#default_value' => $this->configuration['display_label'],
  ];
  $form['round'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Round tax at the order item level'),
    '#description' => $this
      ->t('Sales taxes are not rounded at the order item level, while VAT-style taxes are rounded.'),
    '#default_value' => $this->configuration['round'],
  ];
  $wrapper_id = Html::getUniqueId('tax-type-ajax-wrapper');
  $form['#prefix'] = '<div id="' . $wrapper_id . '">';
  $form['#suffix'] = '</div>';

  // Ajax callbacks need rates and territories to be in form state.
  if (!$form_state
    ->get('tax_form_initialized')) {
    $rates = $this->configuration['rates'];
    $territories = $this->configuration['territories'];

    // Initialize empty rows in case there's no data yet.
    $rates = $rates ?: [
      NULL,
    ];
    $territories = $territories ?: [
      NULL,
    ];
    $form_state
      ->set('rates', $rates);
    $form_state
      ->set('territories', $territories);
    $form_state
      ->set('tax_form_initialized', TRUE);
  }
  $form['rates'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Tax rate'),
      $this
        ->t('Percentage'),
      $this
        ->t('Operations'),
    ],
    '#input' => FALSE,
  ];
  foreach ($form_state
    ->get('rates') as $index => $rate) {
    $rate_form =& $form['rates'][$index];
    $rate_form['rate']['id'] = [
      '#type' => 'value',
      '#value' => $rate ? $rate['id'] : $this->uuidGenerator
        ->generate(),
    ];
    $rate_form['rate']['label'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Name'),
      '#default_value' => $rate ? $rate['label'] : '',
      '#maxlength' => 255,
      '#required' => TRUE,
    ];
    $rate_form['percentage'] = [
      '#type' => 'commerce_number',
      '#title' => $this
        ->t('Percentage'),
      '#default_value' => $rate ? $rate['percentage'] * 100 : 0,
      '#field_suffix' => $this
        ->t('%'),
      '#min' => 0,
      '#max' => 100,
    ];
    $rate_form['remove'] = [
      '#type' => 'submit',
      '#name' => 'remove_rate' . $index,
      '#value' => $this
        ->t('Remove'),
      '#limit_validation_errors' => [],
      '#submit' => [
        [
          get_class($this),
          'removeRateSubmit',
        ],
      ],
      '#rate_index' => $index,
      '#ajax' => [
        'callback' => [
          get_class($this),
          'ajaxCallback',
        ],
        'wrapper' => $wrapper_id,
      ],
    ];
  }
  $form['rates'][] = [
    'add_rate' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add rate'),
      '#submit' => [
        [
          get_class($this),
          'addRateSubmit',
        ],
      ],
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'ajaxCallback',
        ],
        'wrapper' => $wrapper_id,
      ],
    ],
  ];
  $form['territories'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Territory'),
      $this
        ->t('Operations'),
    ],
    '#input' => FALSE,
    '#prefix' => '<p>' . $this
      ->t('The tax type will be used if both the customer and the store belong to one of the territories.') . '</p>',
  ];
  foreach ($form_state
    ->get('territories') as $index => $territory) {
    $territory_form =& $form['territories'][$index];
    $territory_form['territory'] = [
      '#type' => 'address_zone_territory',
      '#default_value' => $territory,
      '#required' => TRUE,
    ];
    $territory_form['remove'] = [
      '#type' => 'submit',
      '#name' => 'remove_territory' . $index,
      '#value' => $this
        ->t('Remove'),
      '#limit_validation_errors' => [],
      '#submit' => [
        [
          get_class($this),
          'removeTerritorySubmit',
        ],
      ],
      '#territory_index' => $index,
      '#ajax' => [
        'callback' => [
          get_class($this),
          'ajaxCallback',
        ],
        'wrapper' => $wrapper_id,
      ],
    ];
  }
  $form['territories'][] = [
    'add_territory' => [
      '#type' => 'submit',
      '#value' => $this
        ->t('Add territory'),
      '#submit' => [
        [
          get_class($this),
          'addTerritorySubmit',
        ],
      ],
      '#limit_validation_errors' => [],
      '#ajax' => [
        'callback' => [
          get_class($this),
          'ajaxCallback',
        ],
        'wrapper' => $wrapper_id,
      ],
    ],
  ];
  return $form;
}