You are here

function commerce_eu_vat_create_instance in Commerce European Union VAT 7

1 call to commerce_eu_vat_create_instance()
commerce_eu_vat_configure_product_type in ./commerce_eu_vat.module
Ensures a EU VAT rate field is present on a product type bundle.

File

./commerce_eu_vat.module, line 94
Code for the Commerce EU VAT.

Code

function commerce_eu_vat_create_instance($field_name, $entity_type, $bundle, $label, $weight = 0, $calculation = FALSE, $display = array()) {

  // If a field type we know should exist isn't found, clear the Field cache.
  if (!field_info_field_types('commerce_tax_rate_reference')) {
    field_cache_clear();
  }

  // Look for or add the specified price field to the requested entity bundle.
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'commerce_tax_rate_reference',
      'cardinality' => -1,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
      'locked' => TRUE,
      'settings' => array(
        'tax_rate_types' => array(
          'eu_vat' => 'eu_vat',
        ),
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $label,
      'required' => FALSE,
      // Because this widget is locked, we need it to use the full price widget
      // since the currency option can't be adjusted at the moment.
      'widget' => array(
        'type' => 'options_select',
        'weight' => $weight,
        'settings' => array(),
      ),
      '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);
  }
}