You are here

commerce_webform.install in Commerce Webform 8

Same filename and directory in other branches
  1. 7.2 commerce_webform.install
  2. 7 commerce_webform.install

Install and uninstall functions for commerce_webform

File

commerce_webform.install
View source
<?php

/**
 * @file
 * Install and uninstall functions for commerce_webform
 */

/**
 * Implements hook_uninstall().
 */
function commerce_webform_uninstall() {

  // Remove the webform fields.
  $field_names = array(
    'field_commerce_webform_nid',
    'field_commerce_webform_sid',
    'commerce_webform_nid',
    'commerce_webform_sid',
  );
  foreach ($field_names as $field_name) {

    // Try and remove all the instances of the field.
    if (function_exists('commerce_line_item_types')) {
      foreach (commerce_line_item_types() as $bundle => $product_type) {
        try {
          $instance = field_read_instance('commerce_line_item', $field_name, $bundle);
          if (!empty($instance)) {
            field_delete_instance($instance);
          }
        } catch (Exception $e) {
          watchdog('commerce_webform', 'Could not remove %field_name field from %instance as %error', array(
            '%field_name' => $field_name,
            '%instance' => $bundle,
            '%error' => $e
              ->getMessage(),
          ), WATCHDOG_WARNING);
        }
      }
    }
    try {

      // Try and remove the field itself.
      $field = field_read_field($field_name);
      if (!empty($field)) {
        field_delete_instance($field);
      }
    } catch (Exception $e) {
      watchdog('commerce_webform', 'Could not remove field %field as %error', array(
        '%field' => $field_name,
        '%error' => $e
          ->getMessage(),
      ), WATCHDOG_WARNING);
    }
  }
  field_purge_batch(1000);

  // Remove the extra columns from the webform table.
  $schema['webform'] = array();
  commerce_webform_schema_alter($schema);
  foreach ($schema['webform']['fields'] as $name => $spec) {
    db_drop_field('webform', $name);
  }
}

/**
 * Add an extra column to the webform table to record if submitting the webform
 * should create a new order or add to the basket.
 */
function commerce_webform_update_7101(&$sandbox) {

  // Run the install function, it has only just been added.
  $schema['webform'] = array();
  commerce_webform_schema_alter($schema);
  db_add_field('commerce_webform_new_order', $schema['webform']['fields']['commerce_webform_new_order']);
}

/**
 * Implements hook_install().
 */
function commerce_webform_install() {
  $schema['webform'] = array();
  commerce_webform_schema_alter($schema);
  foreach ($schema['webform']['fields'] as $name => $spec) {
    db_add_field('webform', $name, $spec);
  }
}

/**
 * Implements hook_schema_alter().
 * Add columns to the webform table.
 */
function commerce_webform_schema_alter(&$schema) {

  // Add field to existing schema.
  $schema['webform']['fields']['commerce_webform_new_order'] = array(
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => '0',
    'description' => 'Boolean value of a commerce webform as to whether it should create a new order (1) or update the current basket (0).',
  );
}

Functions

Namesort descending Description
commerce_webform_install Implements hook_install().
commerce_webform_schema_alter Implements hook_schema_alter(). Add columns to the webform table.
commerce_webform_uninstall Implements hook_uninstall().
commerce_webform_update_7101 Add an extra column to the webform table to record if submitting the webform should create a new order or add to the basket.