You are here

function commerce_license_configure_product_types in Commerce License 7

Ensures that the provided product types have the required license fields.

Fields:

  • commerce_license_type: a list(text) field pointing to a license type.
  • commerce_license_duration: a text field storing strtotime values.

Parameters

$types: An array of product type machine names.

1 call to commerce_license_configure_product_types()
commerce_license_flush_caches in ./commerce_license.module
Implements hook_flush_caches().

File

./commerce_license.module, line 775
Provides a framework for selling access to local or remote resources.

Code

function commerce_license_configure_product_types($types) {
  field_cache_clear();
  $field = field_info_field('commerce_license_type');
  if (!$field) {
    $field = array(
      'field_name' => 'commerce_license_type',
      'type' => 'list_text',
      'locked' => TRUE,
      'settings' => array(
        'allowed_values_function' => 'commerce_license_types_allowed_values',
      ),
    );
    field_create_field($field);
  }
  $existing = array();
  if (!empty($field['bundles']['commerce_product'])) {
    $existing = $field['bundles']['commerce_product'];
  }

  // Create instances on newly configured product types.
  foreach (array_diff($types, $existing) as $new_bundle) {
    $instance = array(
      'field_name' => 'commerce_license_type',
      'entity_type' => 'commerce_product',
      'bundle' => $new_bundle,
      'label' => t('License type'),
      'required' => TRUE,
      'widget' => array(
        'type' => 'options_select',
      ),
    );
    field_create_instance($instance);
  }

  // Remove instances from product types that can no longer have licenses.
  foreach (array_diff($existing, $types) as $removed_bundle) {
    $instance = field_info_instance('commerce_product', 'commerce_license_type', $removed_bundle);
    field_delete_instance($instance, TRUE);
  }
  $field = field_info_field('commerce_license_duration');
  if (!$field) {
    $field = array(
      'field_name' => 'commerce_license_duration',
      'type' => 'number_integer',
      'locked' => TRUE,
    );
    field_create_field($field);
  }
  $existing = array();
  if (!empty($field['bundles']['commerce_product'])) {
    $existing = $field['bundles']['commerce_product'];
  }

  // Create instances on newly configured product types.
  foreach (array_diff($types, $existing) as $new_bundle) {
    $instance = array(
      'field_name' => 'commerce_license_duration',
      'entity_type' => 'commerce_product',
      'bundle' => $new_bundle,
      'label' => t('License duration'),
      'required' => TRUE,
      'widget' => array(
        'type' => 'commerce_license_duration',
      ),
    );
    field_create_instance($instance);
  }

  // Remove instances from product types that can no longer have licenses.
  foreach (array_diff($existing, $types) as $removed_bundle) {
    $instance = field_info_instance('commerce_product', 'commerce_license_duration', $removed_bundle);
    field_delete_instance($instance, TRUE);
  }
}