You are here

function commerce_tax_field_widget_form_alter in Commerce Core 7

Implements hook_field_widget_form_alter().

Alter price widgets on the product form to have tax inclusive price entry. This hook was added in Drupal 7.8, so entering prices including VAT will require at least that version.

File

modules/tax/commerce_tax.module, line 358
Defines tax rates and Rules integration for configuring tax rules for applicability and display.

Code

function commerce_tax_field_widget_form_alter(&$element, &$form_state, $context) {

  // Act on widgets for fields of type commerce_price on commerce_products.
  if ($context['field']['type'] == 'commerce_price' && $context['instance']['entity_type'] == 'commerce_product') {

    // Build an array of tax types that are display inclusive.
    $inclusive_types = array();
    foreach (commerce_tax_types() as $name => $tax_type) {
      if ($tax_type['display_inclusive']) {
        $inclusive_types[$name] = $tax_type['title'];
      }
    }

    // Build an options array of tax rates of these types.
    $options = array();
    foreach (commerce_tax_rates() as $name => $tax_rate) {
      if (array_key_exists($tax_rate['type'], $inclusive_types)) {
        $options[$inclusive_types[$tax_rate['type']]][$name] = t('Including !title', array(
          '!title' => $tax_rate['title'],
        ));
      }
    }
    if (!empty($options)) {

      // Find the default value for the tax included element.
      $default = '';
      if (!empty($element['data']['#default_value']['include_tax'])) {
        $default = $element['data']['#default_value']['include_tax'];
      }
      $element['currency_code']['#title'] = ' ';

      // Note that because this is a select element, the values in the
      // #options array have not been sanitized. They will be passed
      // through check_plain() in form_select_options() when this form
      // element is processed. If you alter the type of this element to
      // radios or checkboxes, you are responsible for sanitizing the
      // values of the #options array as well.
      $element['include_tax'] = array(
        '#type' => 'select',
        '#title' => t('Include tax in this price'),
        '#description' => t('Saving prices tax inclusive will bypass later calculations for the specified tax.'),
        '#options' => count($options) == 1 ? reset($options) : $options,
        '#default_value' => $default,
        '#required' => FALSE,
        '#empty_value' => '',
        '#suffix' => '<div class="commerce-price-tax-included-clearfix"></div>',
        '#attached' => array(
          'css' => array(
            drupal_get_path('module', 'commerce_tax') . '/theme/commerce_tax.theme.css',
          ),
        ),
      );
    }

    // Append a validation handler to the price field's element validate
    // array to add the included tax price component after the price has
    // been converted from a decimal.
    $element['#element_validate'][] = 'commerce_tax_price_field_validate';
  }
}