protected function TaxRateDefaultWidget::getOptions in Commerce Product Tax 8
Returns the array of options for the widget.
Parameters
\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity for which to return options.
Return value
array The array of options for the widget.
Overrides OptionsWidgetBase::getOptions
File
- src/
Plugin/ Field/ FieldWidget/ TaxRateDefaultWidget.php, line 36
Class
- TaxRateDefaultWidget
- Plugin implementation of the 'commerce_tax_rate_default' widget.
Namespace
Drupal\commerce_product_tax\Plugin\Field\FieldWidgetCode
protected function getOptions(FieldableEntityInterface $entity) {
if (isset($this->options)) {
return $this->options;
}
$tax_type = $this
->getFieldSetting('tax_type');
$this->options = [];
// Add an empty option if the widget needs one.
if ($empty_label = $this
->getEmptyLabel()) {
$this->options = [
'_none' => $empty_label,
];
}
$no_applicable_tax = TaxRateResolverInterface::NO_APPLICABLE_TAX_RATE;
// Avoid instantiating the same labels dozens of times.
$no_applicable_tax_label = $this
->t('No tax');
if (!$tax_type) {
return $this->options;
}
$tax_type = TaxType::load($tax_type);
/** @var \Drupal\commerce_tax\Plugin\Commerce\TaxType\LocalTaxTypeInterface $tax_type_plugin */
$tax_type_plugin = $tax_type
->getPlugin();
foreach ($tax_type_plugin
->getZones() as $zone) {
if (!$this
->isAllowed($tax_type, $zone)) {
continue;
}
$label = (string) $zone
->getLabel();
$this->options[$label] = [
$zone
->getId() . '|' . $no_applicable_tax => $no_applicable_tax_label,
];
foreach ($zone
->getRates() as $rate) {
$rate_label = $this
->t('@label', [
'@label' => $rate
->getLabel(),
]);
if ($percentage = $rate
->getPercentage()) {
$rate_label = $this
->t('@label (@percentage%)', [
'@label' => $rate
->getLabel(),
'@percentage' => Calculator::multiply($percentage
->getNumber(), '100'),
]);
}
$this->options[$label][$zone
->getId() . '|' . $rate
->getId()] = $rate_label;
}
}
return $this->options;
}