You are here

function commerce_recurring_configure_recurring_entity_type in Commerce Recurring Framework 7.2

Ensures that the fields for the recurring entity are created.

1 call to commerce_recurring_configure_recurring_entity_type()
commerce_recurring_enable in ./commerce_recurring.install
Implements hook_enable().

File

./commerce_recurring.module, line 150
Commerce recurring module file.

Code

function commerce_recurring_configure_recurring_entity_type() {

  // Product bundle.
  // Create a price field to save the price of the product at the moment of
  // the entity creation.
  commerce_recurring_price_create_instance('commerce_recurring_fixed_price', 'commerce_recurring', 'product', t('Fixed price'), 0, 'calculated_sell_price');

  // Entity reference fields for relating the recurring entity to the product
  // that is recurring and also with the orders.
  $fields = array(
    'commerce_recurring_ref_product' => array(
      'field_name' => 'commerce_recurring_ref_product',
      'type' => 'entityreference',
      'cardinality' => 1,
      'entity_types' => array(
        'commerce_recurring',
      ),
      'module' => 'commerce_recurring',
      'translatable' => FALSE,
      'settings' => array(
        'target_type' => 'commerce_product',
        'handler' => 'base',
        'handler_settings' => array(),
      ),
    ),
    'commerce_recurring_order' => array(
      'field_name' => 'commerce_recurring_order',
      'type' => 'entityreference',
      'cardinality' => FIELD_CARDINALITY_UNLIMITED,
      'entity_types' => array(
        'commerce_recurring',
      ),
      'module' => 'commerce_recurring',
      'translatable' => FALSE,
      'settings' => array(
        'target_type' => 'commerce_order',
        'handler' => 'base',
        'handler_settings' => array(),
      ),
    ),
  );

  // ER instances.
  $basic_instance = array(
    'entity_type' => 'commerce_recurring',
    'bundle' => 'product',
    'required' => TRUE,
    'settings' => array(),
    'display' => array(),
    'widget' => array(
      'type' => 'entityreference_autocomplete',
      'module' => 'entityreference',
    ),
  );
  $instances = array(
    'commerce_recurring_ref_product' => $basic_instance,
    'commerce_recurring_order' => $basic_instance,
  );
  $instances['commerce_recurring_ref_product']['label'] = t('Referenced product');
  $instances['commerce_recurring_order']['label'] = t('Referenced orders');

  // Create fields & instances if necessary.
  foreach ($fields as $field_name => $field) {
    $info_field = field_info_field($field_name);
    $instance = field_info_instance('commerce_recurring', $field_name, 'product');
    if (empty($info_field)) {
      field_create_field($field);
    }
    if (empty($instance)) {
      $instance = $instances[$field_name];
      $instance['field_name'] = $field_name;
      field_create_instance($instance);
    }
  }

  // @TODO Order bundle.
}