You are here

commerce_vat_field.module in Commerce VAT 7

File

commerce_vat_field/commerce_vat_field.module
View source
<?php

/**
 * Implements hook_flush_caches().
 */
function commerce_vat_field_flush_caches() {
  $entity_type = 'commerce_product';
  field_cache_clear();
  foreach (commerce_vat_countries() as $name => $vat_country) {
    if ($vat_country['default_field']) {
      $field_name = 'commerce_' . drupal_strtolower($vat_country['iso2']) . '_vat';
      $field = field_info_field($field_name);
      if (empty($field)) {
        _commerce_vat_field_create_field($field_name, $vat_country, $entity_type);
      }
      $product_types = commerce_product_types();
      foreach ($product_types as $bundle => $product_type) {
        $instance = field_info_instance($entity_type, $field_name, $bundle);
        if (empty($instance)) {
          _commerce_vat_field_create_instance($field_name, $vat_country, $entity_type, $bundle);
        }
      }
    }
  }
}

/**
 * Implements hook_commerce_product_type_insert().
 */
function commerce_vat_field_commerce_product_type_insert($product_type, $skip_reset) {
  $entity_type = 'commerce_product';
  foreach (commerce_vat_countries() as $name => $vat_country) {
    if ($vat_country['default_field']) {
      $field_name = 'commerce_' . drupal_strtolower($vat_country['iso2']) . '_vat';
      $field = field_info_field($field_name);
      if (empty($field)) {
        _commerce_vat_field_create_field($field_name, $vat_country, $entity_type);
      }
      $instance = field_info_instance($entity_type, $field_name, $product_type['type']);
      if (empty($instance)) {
        _commerce_vat_field_create_instance($field_name, $vat_country, $entity_type, $product_type['type']);
      }
    }
  }
}
function _commerce_vat_field_create_field($field_name, $vat_country, $entity_type) {
  if (!field_info_field_types('commerce_vat_rate_reference')) {
    field_cache_clear();
  }
  $field = array(
    'field_name' => $field_name,
    'type' => 'commerce_vat_rate_reference',
    'cardinality' => 1,
    'entity_types' => array(
      $entity_type,
    ),
    'translatable' => FALSE,
    'locked' => FALSE,
    'settings' => array(
      'vat_country' => array(
        $vat_country['iso2'] => $vat_country['iso2'],
      ),
    ),
  );
  $field = field_create_field($field);
}
function _commerce_vat_field_create_instance($field_name, $vat_country, $entity_type, $bundle) {
  $instance = array(
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'label' => t('@country VAT Rates', array(
      '@country' => $vat_country['title'],
    )),
    'required' => FALSE,
    'widget' => array(
      'type' => 'options_select',
    ),
    'display' => array(),
  );

  //     $entity_info = entity_get_info($entity_type);
  //     // Spoof the default view mode and node teaser so its display type is set.
  //     $entity_info['view modes'] += array(
  //       'default' => array(),
  //       'node_teaser' => array(),
  //     );
  //     foreach ($entity_info['view modes'] as $view_mode => $data) {
  //       $instance['display'][$view_mode] = $display + array(
  //         'label' => 'hidden',
  //         'type' => 'commerce_price_formatted_amount',
  //         'settings' => array(
  //           'calculation' => $calculation,
  //         ),
  //         'weight' => $weight,
  //       );
  //     }
  field_create_instance($instance);
}