View source
<?php
namespace Drupal\commerce_tax\Form;
use Drupal\commerce\InlineFormManager;
use Drupal\commerce_tax\TaxTypeManager;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class TaxTypeForm extends EntityForm {
protected $pluginManager;
protected $inlineFormManager;
public function __construct(TaxTypeManager $plugin_manager, InlineFormManager $inline_form_manager) {
$this->pluginManager = $plugin_manager;
$this->inlineFormManager = $inline_form_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.commerce_tax_type'), $container
->get('plugin.manager.commerce_inline_form'));
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$type = $this->entity;
$plugins = array_column($this->pluginManager
->getDefinitions(), 'label', 'id');
asort($plugins);
unset($plugins['custom']);
$plugins = [
'custom' => $this
->t('Custom'),
] + $plugins;
if (!$type
->getPluginId()) {
$plugin_ids = array_keys($plugins);
$plugin = reset($plugin_ids);
$type
->setPluginId($plugin);
}
$plugin = $form_state
->getValue('plugin', $type
->getPluginId());
$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;
}
public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$type = $this->entity;
$type
->setPluginConfiguration($form_state
->getValue([
'configuration',
]));
}
public function save(array $form, FormStateInterface $form_state) {
$this->entity
->save();
$this
->messenger()
->addMessage($this
->t('Saved the %label tax type.', [
'%label' => $this->entity
->label(),
]));
$form_state
->setRedirect('entity.commerce_tax_type.collection');
}
}