You are here

function commerce_license_role_configure_product_types in Commerce License 7

Ensures that the provided product types have the required fields.

Fields:

  • commerce_license_role: a a list(text) field pointing to a role.

Parameters

$types: An array of product type machine names.

1 call to commerce_license_role_configure_product_types()
commerce_license_role_flush_caches in modules/commerce_license_role/commerce_license_role.module
Implements hook_flush_caches().

File

modules/commerce_license_role/commerce_license_role.module, line 78
Provides a license type for selling roles.

Code

function commerce_license_role_configure_product_types($types) {
  $field = field_info_field('commerce_license_role');
  if (!$field) {
    $field = array(
      'field_name' => 'commerce_license_role',
      'cardinality' => 1,
      'type' => 'list_text',
      'locked' => TRUE,
      'settings' => array(
        'allowed_values_function' => 'commerce_license_role_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_role',
      'entity_type' => 'commerce_product',
      'bundle' => $new_bundle,
      'label' => t('Role'),
      'required' => TRUE,
      'widget' => array(
        'type' => 'options_select',
        'weight' => 9,
      ),
    );
    field_create_instance($instance);
  }

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