View source
<?php
namespace Drupal\commerce_product_tax\Plugin\Field\FieldType;
use Drupal\commerce\EntityHelper;
use Drupal\commerce_tax\Entity\TaxType;
use Drupal\commerce_tax\Entity\TaxTypeInterface;
use Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\TypedData\DataDefinition;
class TaxRateItem extends FieldItemBase {
public static function defaultFieldSettings() {
return [
'tax_type' => '',
'allowed_zones' => [],
] + parent::defaultFieldSettings();
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Tax rate'))
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => '64',
],
],
'indexes' => [
'value' => [
'value',
],
],
];
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
$field = $form_state
->getFormObject()
->getEntity();
$element = [
'#type' => 'container',
'#id' => 'form-settings-wrapper',
];
$tax_types = TaxType::loadMultiple();
$tax_types = array_filter($tax_types, function (TaxTypeInterface $tax_type) {
$tax_type_plugin = $tax_type
->getPlugin();
return $tax_type_plugin instanceof LocalTaxTypeInterface;
});
$options = EntityHelper::extractLabels($tax_types);
$default_value = $field
->getSetting('tax_type') ?: key($options);
$element['tax_type'] = [
'#type' => 'select',
'#title' => t('Tax type'),
'#default_value' => $default_value,
'#options' => $options,
'#required' => TRUE,
'#ajax' => [
'callback' => [
get_class($this),
'ajaxRefresh',
],
'wrapper' => 'form-settings-wrapper',
],
];
if ($default_value) {
$tax_type = $tax_types[$default_value];
$tax_type_plugin = $tax_type
->getPlugin();
$tax_type_plugin_id = $tax_type
->getPluginId();
$allowed_zones = [];
foreach ($tax_type_plugin
->getZones() as $zone) {
if ($tax_type_plugin_id == 'european_union_vat' && $zone
->getId() == 'ic') {
continue;
}
$allowed_zones[$zone
->getId()] = $zone
->getLabel();
}
$element['allowed_zones'] = [
'#type' => 'select',
'#title' => t('Allowed zones'),
'#default_value' => $field
->getSetting('allowed_zones'),
'#options' => $allowed_zones,
'#multiple' => TRUE,
];
}
return $element;
}
public static function ajaxRefresh(array $form, FormStateInterface $form_state) {
return $form['settings'];
}
}