class Custom in Commerce Core 8.2
Provides the Custom tax type.
Plugin annotation
@CommerceTaxType(
id = "custom",
label = "Custom",
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase implements TaxTypeInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeBase implements LocalTaxTypeInterface
- class \Drupal\commerce_tax\Plugin\Commerce\TaxType\Custom
- class \Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeBase implements LocalTaxTypeInterface
- class \Drupal\commerce_tax\Plugin\Commerce\TaxType\TaxTypeBase implements TaxTypeInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of Custom
1 file declares its use of Custom
- CustomTest.php in modules/
tax/ tests/ src/ Kernel/ Plugin/ Commerce/ TaxType/ CustomTest.php
4 string references to 'Custom'
- commerce_order.commerce_adjustment_types.yml in modules/
order/ commerce_order.commerce_adjustment_types.yml - modules/order/commerce_order.commerce_adjustment_types.yml
- CustomTest::testTaxTypeCustom in modules/
tax/ tests/ src/ FunctionalJavascript/ CustomTest.php - Tests the custom plugin.
- OrderAdminTest::testEditOrder in modules/
order/ tests/ src/ FunctionalJavascript/ OrderAdminTest.php - Tests editing an order.
- TaxTypeForm::form in modules/
tax/ src/ Form/ TaxTypeForm.php - Gets the actual form array to be built.
File
- modules/
tax/ src/ Plugin/ Commerce/ TaxType/ Custom.php, line 23
Namespace
Drupal\commerce_tax\Plugin\Commerce\TaxTypeView source
class Custom extends LocalTaxTypeBase {
/**
* The UUID generator.
*
* @var \Drupal\Component\Uuid\UuidInterface
*/
protected $uuidGenerator;
/**
* Constructs a new Custom object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\commerce_price\RounderInterface $rounder
* The rounder.
* @param \Drupal\commerce_tax\Resolver\ChainTaxRateResolverInterface $chain_rate_resolver
* The chain tax rate resolver.
* @param \Drupal\Component\Uuid\UuidInterface $uuid_generator
* The UUID generator.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, RounderInterface $rounder, ChainTaxRateResolverInterface $chain_rate_resolver, UuidInterface $uuid_generator) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $event_dispatcher, $rounder, $chain_rate_resolver);
$this->uuidGenerator = $uuid_generator;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('event_dispatcher'), $container
->get('commerce_price.rounder'), $container
->get('commerce_tax.chain_tax_rate_resolver'), $container
->get('uuid'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'display_label' => 'tax',
'round' => TRUE,
'rates' => [],
'territories' => [],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
parent::setConfiguration($configuration);
foreach ($this->configuration['rates'] as &$rate) {
if (isset($rate['amount'])) {
// The 'amount' key was renamed to 'percentage' in 2.0-rc2.
$rate['percentage'] = $rate['amount'];
unset($rate['amount']);
}
}
}
/**
* {@inheritdoc}
*/
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;
}
/**
* Ajax callback for tax rate and zone territory operations.
*/
public static function ajaxCallback(array $form, FormStateInterface $form_state) {
return $form['configuration'];
}
/**
* Submit callback for adding a new rate.
*/
public static function addRateSubmit(array $form, FormStateInterface $form_state) {
$rates = $form_state
->get('rates');
$rates[] = [];
$form_state
->set('rates', $rates);
$form_state
->setRebuild();
}
/**
* Submit callback for removing a rate.
*/
public static function removeRateSubmit(array $form, FormStateInterface $form_state) {
$rates = $form_state
->get('rates');
$index = $form_state
->getTriggeringElement()['#rate_index'];
unset($rates[$index]);
$form_state
->set('rates', $rates);
$form_state
->setRebuild();
}
/**
* Submit callback for adding a new territory.
*/
public static function addTerritorySubmit(array $form, FormStateInterface $form_state) {
$territories = $form_state
->get('territories');
$territories[] = [];
$form_state
->set('territories', $territories);
$form_state
->setRebuild();
}
/**
* Submit callback for removing a territory.
*/
public static function removeTerritorySubmit(array $form, FormStateInterface $form_state) {
$territories = $form_state
->get('territories');
$index = $form_state
->getTriggeringElement()['#territory_index'];
unset($territories[$index]);
$form_state
->set('territories', $territories);
$form_state
->setRebuild();
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue($form['#parents']);
// Filter out the button rows.
$values['rates'] = array_filter($values['rates'], function ($rate) {
return !empty($rate) && !isset($rate['add_rate']);
});
$values['territories'] = array_filter($values['territories'], function ($territory) {
return !empty($territory) && !isset($territory['add_territory']);
});
$form_state
->setValue($form['#parents'], $values);
if (empty($values['rates'])) {
$form_state
->setError($form['rates'], $this
->t('Please add at least one rate.'));
}
if (empty($values['territories'])) {
$form_state
->setError($form['territories'], $this
->t('Please add at least one territory.'));
}
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['display_label'] = $values['display_label'];
$this->configuration['round'] = $values['round'];
$this->configuration['rates'] = [];
foreach (array_filter($values['rates']) as $rate) {
$this->configuration['rates'][] = [
'id' => $rate['rate']['id'],
'label' => $rate['rate']['label'],
'percentage' => (string) ($rate['percentage'] / 100),
];
}
$this->configuration['territories'] = [];
foreach (array_filter($values['territories']) as $territory) {
$this->configuration['territories'][] = $territory['territory'];
}
}
}
/**
* Gets the available display labels.
*
* @return array
* The display labels, keyed by machine name.
*/
protected function getDisplayLabels() {
return [
'tax' => $this
->t('Tax'),
'vat' => $this
->t('VAT'),
// Australia, New Zealand, Singapore, Hong Kong, India, Malaysia.
'gst' => $this
->t('GST'),
// Japan.
'consumption_tax' => $this
->t('Consumption tax'),
];
}
/**
* Gets the configured display label.
*
* @return string
* The configured display label.
*/
protected function getDisplayLabel() {
$display_labels = $this
->getDisplayLabels();
$display_label_id = $this->configuration['display_label'];
if (isset($display_labels[$display_label_id])) {
$display_label = $display_labels[$display_label_id];
}
else {
$display_label = reset($display_labels);
}
return $display_label;
}
/**
* {@inheritdoc}
*/
public function shouldRound() {
return $this->configuration['round'];
}
/**
* {@inheritdoc}
*/
public function buildZones() {
$rates = $this->configuration['rates'];
// The plugin doesn't support defining multiple percentages with own
// start/end dates for UX reasons, so a start date is invented here.
foreach ($rates as &$rate) {
$rate['percentages'][] = [
'number' => (string) $rate['percentage'],
'start_date' => '2000-01-01',
];
unset($rate['percentage']);
}
// The first defined rate is assumed to be the default.
$rates[0]['default'] = TRUE;
$zones = [];
$zones['default'] = new TaxZone([
'id' => 'default',
'label' => 'Default',
'display_label' => $this
->getDisplayLabel(),
'territories' => $this->configuration['territories'],
'rates' => $rates,
]);
return $zones;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Custom:: |
protected | property | The UUID generator. | |
Custom:: |
public static | function | Submit callback for adding a new rate. | |
Custom:: |
public static | function | Submit callback for adding a new territory. | |
Custom:: |
public static | function | Ajax callback for tax rate and zone territory operations. | |
Custom:: |
public | function |
Form constructor. Overrides TaxTypeBase:: |
|
Custom:: |
public | function |
Builds the tax zones. Overrides LocalTaxTypeBase:: |
|
Custom:: |
public static | function |
Creates an instance of the plugin. Overrides LocalTaxTypeBase:: |
|
Custom:: |
public | function |
Gets default configuration for this plugin. Overrides TaxTypeBase:: |
|
Custom:: |
protected | function | Gets the configured display label. | |
Custom:: |
protected | function | Gets the available display labels. | |
Custom:: |
public static | function | Submit callback for removing a rate. | |
Custom:: |
public static | function | Submit callback for removing a territory. | |
Custom:: |
public | function |
Sets the configuration for this plugin instance. Overrides TaxTypeBase:: |
|
Custom:: |
public | function |
Gets whether tax should be rounded at the order item level. Overrides LocalTaxTypeBase:: |
|
Custom:: |
public | function |
Form submission handler. Overrides TaxTypeBase:: |
|
Custom:: |
public | function |
Form validation handler. Overrides TaxTypeBase:: |
|
Custom:: |
public | function |
Constructs a new Custom object. Overrides LocalTaxTypeBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
LocalTaxTypeBase:: |
protected | property | The chain tax rate resolver. | |
LocalTaxTypeBase:: |
protected | property | The matched zones. | |
LocalTaxTypeBase:: |
protected | property | The rounder. | |
LocalTaxTypeBase:: |
protected | property | The zones. | |
LocalTaxTypeBase:: |
public | function |
Checks whether the tax type applies to the given order. Overrides TaxTypeBase:: |
|
LocalTaxTypeBase:: |
public | function |
Applies the tax type to the given order. Overrides TaxTypeInterface:: |
|
LocalTaxTypeBase:: |
protected | function | Builds the summary of all available tax rates. | |
LocalTaxTypeBase:: |
protected | function | Checks whether the store is registered to collect taxes in the given zone. | |
LocalTaxTypeBase:: |
public | function |
Gets the tax zones which match the given address. Overrides LocalTaxTypeInterface:: |
|
LocalTaxTypeBase:: |
public | function |
Gets the tax zones. Overrides LocalTaxTypeInterface:: |
|
LocalTaxTypeBase:: |
protected | function | Checks whether the tax type matches the store's billing address. | 1 |
LocalTaxTypeBase:: |
protected | function | Checks whether the tax type matches the store's tax registrations. | 1 |
LocalTaxTypeBase:: |
protected | function | Resolves the tax rates for the given order item and customer profile. | |
LocalTaxTypeBase:: |
protected | function | Resolves the tax zones for the given order item and customer profile. | 2 |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
TaxTypeBase:: |
protected | property | The ID of the parent config entity. | |
TaxTypeBase:: |
protected | property | The entity type manager. | |
TaxTypeBase:: |
protected | property | The event dispatcher. | |
TaxTypeBase:: |
protected | property | The parent config entity. | |
TaxTypeBase:: |
protected | property | A cache of prepared customer profiles, keyed by order ID. | |
TaxTypeBase:: |
protected | function | Builds a customer profile for the given order. | |
TaxTypeBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
TaxTypeBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
TaxTypeBase:: |
public | function |
Gets the tax type label. Overrides TaxTypeInterface:: |
|
TaxTypeBase:: |
protected | function | Gets the taxable type for the given order item. | |
TaxTypeBase:: |
public | function |
Gets the tax type weight. Overrides TaxTypeInterface:: |
|
TaxTypeBase:: |
public | function |
Gets whether the tax type is display inclusive. Overrides TaxTypeInterface:: |
|
TaxTypeBase:: |
protected | function | Resolves the customer profile for the given order item. | |
TaxTypeBase:: |
public | function |
Overrides DependencySerializationTrait:: |
|
TaxTypeBase:: |
public | function |
Overrides DependencySerializationTrait:: |