You are here

commerce_vat_reference.module in Commerce VAT 7

File

commerce_vat_reference/commerce_vat_reference.module
View source
<?php

/**
 * Implements hook_field_info().
 */
function commerce_vat_reference_field_info() {
  return array(
    'commerce_vat_rate_reference' => array(
      'label' => t('VAT rate reference'),
      'description' => t('This field a reference to a vat rate.'),
      'settings' => array(
        'vat_country' => array(),
      ),
      'instance_settings' => array(),
      'default_widget' => 'commerce_vat_rate_reference',
      'default_formatter' => 'commerce_vat_rate',
      'property_type' => 'text',
    ),
  );
}

/**
 * Implements hook_field_widget_info_alter().
 */
function commerce_vat_reference_field_widget_info_alter(&$info) {
  $info['options_select']['field types'][] = 'commerce_vat_rate_reference';
  $info['options_buttons']['field types'][] = 'commerce_vat_rate_reference';
}

/**
 * Implements hook_field_formatter_info().
 */
function commerce_vat_reference_field_formatter_info() {
  return array(
    'commerce_vat_rate' => array(
      'label' => t('Default'),
      'field types' => array(
        'commerce_vat_rate_reference',
      ),
    ),
  );
}

/**
 * Implements hook_options_list().
 */
function commerce_vat_reference_options_list($field, $instance, $entity_type, $entity) {
  if ($field['type'] == 'commerce_vat_rate_reference') {
    $vat_rates = commerce_vat_rates();

    // Get the tax rate types we limit the widget by, if any.
    // Array will be empty if all are to be included.
    $limit_vat_country = array_filter($field['settings']['vat_country']);
    $options = array();
    foreach ($vat_rates as $vat_rate_name => $vat_rate_info) {
      if (count($limit_vat_country)) {
        if (!isset($limit_vat_country[drupal_strtoupper($vat_rate_info['country'])])) {

          // Skip a tax rate if its type isn't one we should limit to.
          continue;
        }
      }
      $options[$vat_rate_name] = $vat_rate_info['title'];
    }
  }
  return $options;
}

/**
 * Implements hook_field_is_empty().
 */
function commerce_vat_reference_field_is_empty($item, $field) {

  // Shouldn't ever come here as options widgets take care of this, but
  // this hook is required.
  if ($field['type'] == 'commerce_vat_rate_reference') {
    return empty($item['value']);
  }
}

/**
 * Implements hook_field_settings_form().
 */
function commerce_vat_reference_field_settings_form($field, $instance, $has_data) {
  if ($field['type'] == 'commerce_vat_rate_reference') {
    $settings = $field['settings'];
    $options = array();
    foreach (commerce_vat_countries() as $iso2 => $vat_country) {
      $options[$iso2] = check_plain($vat_country['title']);
    }
    $form['vat_country'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Available tax countries'),
      '#options' => $options,
      '#default_value' => is_array($settings['vat_country']) ? $settings['vat_country'] : array(),
      '#description' => t('Limit the tax rates that can be selected to those of the countries listed. Leave blank to make all tax rates available.'),
    );
  }
  return $form;
}

/**
 * Implements hook_field_formatter_view().
 */
function commerce_vat_reference_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'commerce_vat_rate':
      $vat_rates = commerce_vat_rates();
      foreach ($items as $delta => $item) {
        $element[$delta] = array(
          '#markup' => $vat_rates[$item['value']]['title'],
        );
      }
      break;
  }
  return $element;
}