public function TaxTypeForm::form in Commerce Core 8.2
Gets the actual form array to be built.
Overrides EntityForm::form
See also
\Drupal\Core\Entity\EntityForm::processForm()
\Drupal\Core\Entity\EntityForm::afterBuild()
File
- modules/
tax/ src/ Form/ TaxTypeForm.php, line 54
Class
Namespace
Drupal\commerce_tax\FormCode
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
/** @var \Drupal\commerce_tax\Entity\TaxTypeInterface $type */
$type = $this->entity;
$plugins = array_column($this->pluginManager
->getDefinitions(), 'label', 'id');
asort($plugins);
// Move the Custom plugin to the front.
unset($plugins['custom']);
$plugins = [
'custom' => $this
->t('Custom'),
] + $plugins;
// Use the first available plugin as the default value.
if (!$type
->getPluginId()) {
$plugin_ids = array_keys($plugins);
$plugin = reset($plugin_ids);
$type
->setPluginId($plugin);
}
// The form state will have a plugin value if #ajax was used.
$plugin = $form_state
->getValue('plugin', $type
->getPluginId());
// Pass the plugin configuration only if the plugin hasn't been changed via #ajax.
$plugin_configuration = $type
->getPluginId() == $plugin ? $type
->getPluginConfiguration() : [];
$wrapper_id = Html::getUniqueId('tax-type-form');
$form['#tree'] = TRUE;
$form['#prefix'] = '<div id="' . $wrapper_id . '">';
$form['#suffix'] = '</div>';
$form['#tree'] = TRUE;
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Name'),
'#maxlength' => 255,
'#default_value' => $type
->label(),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $type
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\commerce_tax\\Entity\\TaxType::load',
],
'#disabled' => !$type
->isNew(),
];
$form['plugin'] = [
'#type' => 'radios',
'#title' => $this
->t('Plugin'),
'#options' => $plugins,
'#default_value' => $plugin,
'#required' => TRUE,
'#disabled' => !$type
->isNew(),
'#limit_validation_errors' => [],
'#ajax' => [
'callback' => '::ajaxRefresh',
'wrapper' => $wrapper_id,
],
];
$inline_form = $this->inlineFormManager
->createInstance('plugin_configuration', [
'plugin_type' => 'commerce_tax_type',
'plugin_id' => $plugin,
'plugin_configuration' => $plugin_configuration,
]);
$form['configuration']['#inline_form'] = $inline_form;
$form['configuration']['#parents'] = [
'configuration',
];
$form['configuration'] = $inline_form
->buildInlineForm($form['configuration'], $form_state);
$form['status'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enabled'),
'#default_value' => $type
->status(),
];
return $form;
}