You are here

commerce_shipping_weight_tariff.install in Commerce Shipping Weight Tariff 7

commerce_shipping_weight_tariff.install Contains install hooks.

File

commerce_shipping_weight_tariff.install
View source
<?php

/**
 * @file commerce_shipping_weight_tariff.install
 * Contains install hooks.
 */

/**
 * Implements hook_schema().
 */
function commerce_shipping_weight_tariff_schema() {
  $schema = array();
  $schema['commerce_shipping_weight_tariff_service'] = array(
    'description' => 'Stores information about shipping services created through the commerce shipping weight tariff module.',
    'fields' => array(
      'name' => array(
        'description' => 'The machine-name of the service.',
        'type' => 'varchar',
        'length' => 32,
        'not null' => TRUE,
        'default' => '',
      ),
      'title' => array(
        'description' => 'The human-readable title of the service.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'display_title' => array(
        'description' => 'The title of the service displayed to customers.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'A brief description of the service.',
        'type' => 'text',
        'not null' => TRUE,
        'size' => 'medium',
      ),
    ),
    'primary key' => array(
      'name',
    ),
  );
  return $schema;
}

/**
 * Implements hook_install().
 */
function commerce_shipping_weight_tariff_install() {
  drupal_install_schema('commerce_shipping_weight_tariff_service');

  // Add the fields on our bundle.
  commerce_shipping_weight_tariff_create_weight_instance('commerce_product_tariff_weight', 'commerce_product', 'shipping_tariff', t('Maximum weight'));
  commerce_shipping_weight_tariff_create_method_instance('commerce_product_tariff_service', 'commerce_product', 'shipping_tariff', t('Shipping service'));
}

/**
 * Creates a required, locked instance of a weight field on the specified bundle.
 *
 * @param $field_name
 *   The name of the field; if it already exists, a new instance of the existing
 *     field will be created. For fields governed by the Commerce modules, this
 *     should begin with commerce_.
 * @param $entity_type
 *   The type of entity the field instance will be attached to.
 * @param $bundle
 *   The bundle name of the entity the field instance will be attached to.
 * @param $label
 *   The label of the field instance.
 * @param $weight
 *   The default weight of the field instance widget and display.
 *
 * @see commerce_product_reference_create_instance().
 */
function commerce_shipping_weight_tariff_create_weight_instance($field_name, $entity_type, $bundle, $label, $weight = 0) {

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

  // Look for or add the specified 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' => 'physical_weight',
      'cardinality' => 1,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
      'locked' => TRUE,
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $label,
      'required' => TRUE,
      'settings' => array(),
      'widget' => array(
        'type' => 'physical_weight_textfield',
        'weight' => $weight,
      ),
      'display' => array(
        'display' => array(
          'label' => 'hidden',
          'weight' => $weight,
        ),
      ),
    );
    field_create_instance($instance);
  }
}

/**
 * Create an instance of the tariff shipping method reference field.
 */
function commerce_shipping_weight_tariff_create_method_instance($field_name, $entity_type, $bundle, $label, $description = '', $weight = 0, $display = array()) {

  // 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' => 'list_text',
      'cardinality' => 1,
      'translatable' => FALSE,
      'locked' => TRUE,
      'entity_types' => array(
        $entity_type,
      ),
      'settings' => array(
        'allowed_values_function' => 'commerce_shipping_weight_tariff_methods_options_list',
      ),
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $label,
      'description' => $description,
      'required' => TRUE,
      'settings' => array(),
      'commerce_cart_settings' => array(
        'attribute_field' => 0,
      ),
      'widget' => array(
        'type' => 'options_select',
        'weight' => $weight,
      ),
      'display' => array(),
    );
    field_create_instance($instance);
  }
}

/**
 * Implements hook_uninstall().
 */
function commerce_shipping_weight_tariff_uninstall() {
  module_load_include('module', 'commerce');

  // Delete any field instance attached to a shipping tariff product type.
  commerce_delete_instances('commerce_product', 'shipping_tariff');
  drupal_uninstall_schema('commerce_shipping_weight_tariff_service');
  variable_del('commerce_shipping_weight_tariff_block_weight_unit');
}

/**
 * Creates table for use in storing services.
 */
function commerce_shipping_weight_tariff_update_7100() {
  $schema = drupal_get_schema('commerce_shipping_weight_tariff_service');
  db_create_table('commerce_shipping_weight_tariff_service', $schema);
}

Functions

Namesort descending Description
commerce_shipping_weight_tariff_create_method_instance Create an instance of the tariff shipping method reference field.
commerce_shipping_weight_tariff_create_weight_instance Creates a required, locked instance of a weight field on the specified bundle.
commerce_shipping_weight_tariff_install Implements hook_install().
commerce_shipping_weight_tariff_schema Implements hook_schema().
commerce_shipping_weight_tariff_uninstall Implements hook_uninstall().
commerce_shipping_weight_tariff_update_7100 Creates table for use in storing services.