|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Provides tax functionality. |
| 6 | + */ |
| 7 | + |
| 8 | +use Drupal\commerce_tax\TaxableType; |
| 9 | +use Drupal\Core\Entity\EntityTypeInterface; |
| 10 | +use Drupal\Core\Field\BaseFieldDefinition; |
| 11 | +use Drupal\Core\Form\FormStateInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Implements hook_entity_base_field_info(). |
| 15 | + */ |
| 16 | +function commerce_tax_entity_base_field_info(EntityTypeInterface $entity_type) { |
| 17 | + if ($entity_type->id() === 'commerce_store') { |
| 18 | + $fields['prices_include_tax'] = BaseFieldDefinition::create('boolean') |
| 19 | + ->setLabel(t('Prices are entered with taxes included.')) |
| 20 | + ->setDisplayOptions('form', [ |
| 21 | + 'type' => 'boolean_checkbox', |
| 22 | + 'settings' => [ |
| 23 | + 'display_label' => TRUE, |
| 24 | + ], |
| 25 | + 'weight' => 3, |
| 26 | + ]) |
| 27 | + ->setDisplayConfigurable('view', TRUE) |
| 28 | + ->setDisplayConfigurable('form', TRUE) |
| 29 | + ->setDefaultValue(FALSE); |
| 30 | + |
| 31 | + $fields['tax_registrations'] = BaseFieldDefinition::create('list_string') |
| 32 | + ->setLabel(t('Tax registrations')) |
| 33 | + ->setDescription(t('The countries where the store is additionally registered to collect taxes.')) |
| 34 | + ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED) |
| 35 | + ->setSetting('allowed_values_function', ['\Drupal\commerce_store\Entity\Store', 'getAvailableCountries']) |
| 36 | + ->setDisplayOptions('form', [ |
| 37 | + 'type' => 'options_select', |
| 38 | + 'weight' => 4, |
| 39 | + ]) |
| 40 | + ->setDisplayConfigurable('view', TRUE) |
| 41 | + ->setDisplayConfigurable('form', TRUE); |
| 42 | + |
| 43 | + return $fields; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * Implements hook_form_BASE_FORM_ID_alter() for 'commerce_store_form'. |
| 49 | + */ |
| 50 | +function commerce_tax_form_commerce_store_form_alter(&$form, FormStateInterface $form_state) { |
| 51 | + if (isset($form['prices_include_tax']) || isset($form['tax_registrations'])) { |
| 52 | + $form['tax_settings'] = [ |
| 53 | + '#title' => t('Tax settings'), |
| 54 | + '#weight' => 99, |
| 55 | + '#type' => 'details', |
| 56 | + '#collapsible' => TRUE, |
| 57 | + '#open' => TRUE, |
| 58 | + ]; |
| 59 | + $form['prices_include_tax']['#group'] = 'tax_settings'; |
| 60 | + $form['tax_registrations']['#group'] = 'tax_settings'; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Implements hook_form_FORM_ID_alter() for 'commerce_order_item_type_form'. |
| 66 | + */ |
| 67 | +function commerce_tax_form_commerce_order_item_type_form_alter(array &$form, FormStateInterface $form_state) { |
| 68 | + /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_item_type */ |
| 69 | + $order_item_type = $form_state->getFormObject()->getEntity(); |
| 70 | + |
| 71 | + $form['commerce_tax'] = [ |
| 72 | + '#type' => 'container', |
| 73 | + '#weight' => 5, |
| 74 | + ]; |
| 75 | + $form['commerce_tax']['taxable_type'] = [ |
| 76 | + '#type' => 'select', |
| 77 | + '#title' => t('Taxable type'), |
| 78 | + '#options' => TaxableType::getLabels(), |
| 79 | + '#default_value' => $order_item_type->getThirdPartySetting('commerce_tax', 'taxable_type', TaxableType::getDefault()), |
| 80 | + '#required' => TRUE, |
| 81 | + ]; |
| 82 | + $form['actions']['submit']['#submit'][] = 'commerce_tax_order_item_type_form_submit'; |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * Submission handler for commerce_tax_form_commerce_order_item_type_form_alter(). |
| 87 | + */ |
| 88 | +function commerce_tax_order_item_type_form_submit($form, FormStateInterface $form_state) { |
| 89 | + /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_item_type */ |
| 90 | + $order_item_type = $form_state->getFormObject()->getEntity(); |
| 91 | + $settings = $form_state->getValue(['commerce_tax']); |
| 92 | + $order_item_type->setThirdPartySetting('commerce_tax', 'taxable_type', $settings['taxable_type']); |
| 93 | + $order_item_type->save(); |
| 94 | +} |
0 commit comments