You are here

function commerce_recurring_price_create_instance in Commerce Recurring Framework 7.2

Creates a instance of a price field on the specified bundle.

Parameters

string $field_name: The name of the field; if it already exists, a new instance of the existing field will be created. For fields governed by the Commerce modules, this should begin with commerce_.

string $entity_type: The type of entity the field instance will be attached to.

string $bundle: The bundle name of the entity the field instance will be attached to.

string $label: The label of the field instance.

int $weight: The default weight of the field instance widget and display.

bool $calculation: A string indicating the default value of the display formatter's calculation setting.

array $display: An array of default display data used for the entity's current view modes.

bool $required: Determines if the field is required.

2 calls to commerce_recurring_price_create_instance()
commerce_recurring_configure_product_type in ./commerce_recurring.module
Ensures that the price and interval fields are created.
commerce_recurring_configure_recurring_entity_type in ./commerce_recurring.module
Ensures that the fields for the recurring entity are created.

File

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

Code

function commerce_recurring_price_create_instance($field_name, $entity_type, $bundle, $label, $weight = 0, $calculation = FALSE, $display = array(), $required = FALSE) {

  // If a field type we know should exist isn't found, clear the Field cache.
  if (!field_info_field_types('commerce_price')) {
    field_cache_clear();
  }

  // Look for or add the specified price field to the requested entity bundle.
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);
  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'commerce_price',
      'cardinality' => 1,
      'entity_types' => array(
        $entity_type,
      ),
      'translatable' => FALSE,
    );
    field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => $entity_type,
      'bundle' => $bundle,
      'label' => $label,
      'required' => $required,
      'settings' => array(),
      // Because this widget is locked, we need it to use the full price widget
      // since the currency option can't be adjusted at the moment.
      'widget' => array(
        'type' => 'commerce_price_full',
        'weight' => $weight,
        'settings' => array(
          'currency_code' => 'default',
        ),
      ),
      'display' => array(),
    );
    $entity_info = entity_get_info($entity_type);

    // Spoof the default view mode and node teaser so its display type is set.
    $entity_info['view modes'] += array(
      'default' => array(),
      'node_teaser' => array(),
    );
    foreach ($entity_info['view modes'] as $view_mode => $data) {
      $instance['display'][$view_mode] = $display + array(
        'label' => 'hidden',
        'type' => 'commerce_price_formatted_amount',
        'settings' => array(
          'calculation' => $calculation,
        ),
        'weight' => $weight,
      );
    }
    field_create_instance($instance);
  }
}