commerce_avatax.install in Drupal Commerce Connector for AvaTax 7.5
Same filename and directory in other branches
Installation functions for Commerce Avatax module.
File
commerce_avatax.installView source
<?php
/**
* @file
* Installation functions for Commerce Avatax module.
*/
/**
* Implements hook_requirements().
*/
function commerce_avatax_requirements($phase) {
// Skip the requirements check if SimpleTest is installed.
if (module_exists('simpletest')) {
return;
}
$requirements = array();
// Ensure translations don't break at install time.
$t = get_t();
$has_curl = extension_loaded('curl');
$requirements['commerce_avatax_curl'] = array(
'title' => $t('cURL'),
'value' => $has_curl ? $t('Enabled') : $t('Not found'),
);
if (!$has_curl) {
$requirements['commerce_avatax_curl']['severity'] = REQUIREMENT_ERROR;
$requirements['commerce_avatax_curl']['description'] = $t('Commerce Avatax requires PHP cURL extension to be enabled and configured on your server.');
}
return $requirements;
}
/**
* Implements hook_install().
*/
function commerce_avatax_install() {
commerce_avatax_install_helper();
}
/**
* Implements hook_uninstall().
*/
function commerce_avatax_uninstall() {
drupal_load('module', 'commerce_avatax');
// Delete the Avatax codes Taxonomy.
if ($vocabulary = taxonomy_vocabulary_machine_name_load('avatax_codes')) {
taxonomy_vocabulary_delete($vocabulary->vid);
}
$fields_to_delete = array(
COMMERCE_AVATAX_TAX_CODE_FIELD,
COMMERCE_AVATAX_EXEMPTION_CODE_FIELD,
COMMERCE_AVATAX_VAT_ID_FIELD,
);
foreach ($fields_to_delete as $field_name) {
// Skip non existing fields.
if (!field_info_field($field_name)) {
continue;
}
field_delete_field($field_name);
}
// Remove all variables defined by Commerce Avatax.
db_delete('variable')
->condition('name', COMMERCE_AVATAX_VAR_PREFIX . '%', 'LIKE')
->execute();
}
/**
* Helper function to define and create the required fields & instances.
*/
function commerce_avatax_install_helper() {
drupal_load('module', 'commerce_avatax');
drupal_load('module', 'commerce_product');
// Configure the Avatax codes Taxonomy vocabulary.
$t = get_t();
$vocabulary = taxonomy_vocabulary_machine_name_load('avatax_codes');
if (!$vocabulary) {
$vocabulary = (object) array(
'name' => $t('AvaTax codes'),
'machine_name' => 'avatax_codes',
);
taxonomy_vocabulary_save($vocabulary);
taxonomy_term_save((object) array(
'name' => 'P0000000',
'vid' => $vocabulary->vid,
));
taxonomy_term_save((object) array(
'name' => 'NT',
'vid' => $vocabulary->vid,
));
}
_commerce_avatax_configure_tax_code_field();
_commerce_avatax_configure_tax_exemption_field();
_commerce_avatax_configure_vat_id_field();
}
/**
* Configure the "tax code" field.
*/
function _commerce_avatax_configure_tax_code_field() {
$field_name = COMMERCE_AVATAX_TAX_CODE_FIELD;
$product_types = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'product_types', array());
// By default add the commerce_avatax_tax_code field to all product bundles.
if (empty($product_types)) {
$product_types = array_keys(commerce_product_types());
}
else {
$all_bundles = array_keys(commerce_product_types());
$types_to_remove = array_diff($all_bundles, $product_types);
foreach ($types_to_remove as $bundle) {
if ($instance = field_info_instance('commerce_product', $field_name, $bundle)) {
field_delete_instance($instance);
}
}
}
// Create the field type if it doesn't exist.
$field = field_info_field($field_name);
if (!$field) {
$field = field_create_field(array(
'cardinality' => 1,
'field_name' => $field_name,
'settings' => array(
'allowed_values' => array(
0 => array(
'vocabulary' => 'avatax_codes',
),
),
),
'type' => 'taxonomy_term_reference',
));
}
// Create the field instance for the selected product types.
foreach ($product_types as $type) {
$instance = field_info_instance('commerce_product', $field_name, $type);
if (!$instance) {
field_create_instance(array(
'bundle' => $type,
// @todo: set default_value
'default_value' => NULL,
'display' => array(
'default' => array(
'type' => 'hidden',
),
'teaser' => array(
'type' => 'hidden',
),
),
'entity_type' => 'commerce_product',
'field_name' => $field_name,
'label' => 'AvaTax code',
'required' => TRUE,
'widget' => array(
'module' => 'taxonomy',
'settings' => array(
'autocomplete_path' => 'taxonomy/autocomplete',
'size' => 60,
),
'type' => 'taxonomy_autocomplete',
'weight' => -4,
),
));
}
}
}
/**
* Helper function : Configure the tax exemption field on users.
*/
function _commerce_avatax_configure_tax_exemption_field() {
$field_name = COMMERCE_AVATAX_EXEMPTION_CODE_FIELD;
$exemption_status = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'exemptions_status', 0);
// Delete the field if it exist.
if (!$exemption_status) {
$instance = field_info_instance('user', $field_name, 'user');
if ($instance) {
field_delete_instance($instance);
}
}
else {
// Exemption status is YES.
// Create the field and instance if they do not exist.
$field = field_info_field($field_name);
if (!$field) {
field_create_field(array(
'cardinality' => 1,
'field_name' => $field_name,
'settings' => array(
'allowed_values_function' => 'commerce_avatax_exemption_codes_allowed_values',
),
'type' => 'list_text',
));
}
$instance = field_info_instance('user', $field_name, 'user');
if (!$instance) {
field_create_instance(array(
'bundle' => 'user',
'display' => array(
'default' => array(
'label' => 'above',
'module' => 'list',
'settings' => array(),
'type' => 'list_default',
'weight' => 0,
),
),
'entity_type' => 'user',
'field_name' => $field_name,
'label' => 'AvaTax Exemption Code',
'required' => 0,
'settings' => array(
'user_register_form' => 0,
),
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(),
'type' => 'options_select',
'weight' => 7,
),
));
}
}
}
/**
* Helper function : Configure the VAT ID field.
*/
function _commerce_avatax_configure_vat_id_field() {
$field_name = COMMERCE_AVATAX_VAT_ID_FIELD;
$add_vat_field = variable_get(COMMERCE_AVATAX_VAR_PREFIX . 'add_vat_field', 0);
// Delete the field if it exist.
if (!$add_vat_field) {
$instance = field_info_instance('user', $field_name, 'user');
if ($instance) {
field_delete_instance($instance);
}
}
else {
// Create the field and instance if they do not exist.
$field = field_info_field($field_name);
if (!$field) {
field_create_field(array(
'cardinality' => 1,
'field_name' => $field_name,
'type' => 'text',
));
}
$instance = field_info_instance('user', $field_name, 'user');
if (!$instance) {
field_create_instance(array(
'bundle' => 'user',
'display' => array(),
'entity_type' => 'user',
'field_name' => $field_name,
'label' => 'VAT ID',
'required' => 0,
'settings' => array(
'user_register_form' => 0,
),
'widget' => array(
'active' => 1,
'module' => 'text',
'settings' => array(),
'type' => 'text_textfield',
'weight' => 7,
),
));
}
}
}
Functions
Name | Description |
---|---|
commerce_avatax_install | Implements hook_install(). |
commerce_avatax_install_helper | Helper function to define and create the required fields & instances. |
commerce_avatax_requirements | Implements hook_requirements(). |
commerce_avatax_uninstall | Implements hook_uninstall(). |
_commerce_avatax_configure_tax_code_field | Configure the "tax code" field. |
_commerce_avatax_configure_tax_exemption_field | Helper function : Configure the tax exemption field on users. |
_commerce_avatax_configure_vat_id_field | Helper function : Configure the VAT ID field. |