You are here

function commerce_license_configure_line_item_types in Commerce License 7

Ensures that the provided line item types have the required license fields.

Fields:

  • commerce_license: an entityreference field pointing to a license.

Parameters

$types: An array of line item type machine names.

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

File

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

Code

function commerce_license_configure_line_item_types($types) {
  $field = field_info_field('commerce_license');
  if (!$field) {
    $field = array(
      'settings' => array(
        'handler' => 'base',
        'target_type' => 'commerce_license',
      ),
      'field_name' => 'commerce_license',
      'type' => 'entityreference',
    );
    field_create_field($field);
  }
  $existing = array();
  if (!empty($field['bundles']['commerce_line_item'])) {
    $existing = $field['bundles']['commerce_line_item'];
  }

  // Create instances on newly configured line item types.
  foreach (array_diff($types, $existing) as $new_bundle) {
    $instance = array(
      'label' => 'License',
      'field_name' => 'commerce_license',
      'entity_type' => 'commerce_line_item',
      'bundle' => $new_bundle,
      'required' => TRUE,
    );

    // Configure IEF, if available.
    if (module_exists('inline_entity_form')) {
      $instance['widget'] = array(
        'type' => 'inline_entity_form_license',
      );
    }
    field_create_instance($instance);
  }

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