You are here

function commerce_webform_uninstall in Commerce Webform 7.2

Same name and namespace in other branches
  1. 8 commerce_webform.install \commerce_webform_uninstall()
  2. 7 commerce_webform.install \commerce_webform_uninstall()

Implements hook_uninstall().

File

./commerce_webform.install, line 11
Install and uninstall functions for commerce_webform

Code

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);
  }
}