You are here

function commerce_multicurrency_handling_settings_form_submit in Commerce Multicurrency 7

Submit handler for the handling settings form.

File

./commerce_multicurrency.admin.inc, line 234
Administrative UI for commerce currency.

Code

function commerce_multicurrency_handling_settings_form_submit($form, &$form_state) {
  foreach ($form_state['values']['currency_fields'] as $commerce_product_type => $currency_fields) {
    foreach ($currency_fields as $currency_code => $has_field) {
      $field_name = 'commerce_price_' . strtolower($currency_code);
      $field_instance = field_info_instance('commerce_product', $field_name, $commerce_product_type);
      switch (TRUE) {

        // Field enabled but none created yet.
        case $has_field && !$field_instance:

          // Reuse commerce code to create the initial field instance.
          commerce_price_create_instance($field_name, 'commerce_product', $commerce_product_type, 'Price in ' . $currency_code);

          // Fetch the new instance and modify it.
          $new_field_instance = field_info_instance('commerce_product', $field_name, $commerce_product_type);
          $new_field_instance['widget']['settings']['currency_code'] = $currency_code;
          $new_field_instance['widget']['type'] = 'commerce_price_simple';
          field_update_instance($new_field_instance);
          drupal_set_message(t('Field %fieldname on %type created.', array(
            '%fieldname' => $field_name,
            '%type' => $commerce_product_type,
          )), 'status', FALSE);
          break;

        // Field enabled and already created.
        case $has_field && !$field_instance:

          // Done.
          break;

        // Field disabled but still there.
        case !$has_field && $field_instance:
          field_delete_instance($field_instance);
          drupal_set_message(t('Field %fieldname on %type deleted.', array(
            '%fieldname' => $field_name,
            '%type' => $commerce_product_type,
          )), 'status', FALSE);
          break;

        // Field disabled and already removed.
        case !$has_field && $field_instance:

          // Done.
          break;
      }
    }
  }
}