You are here

function commerce_donate_commerce_donate_line_item_configuration in Commerce Donate 7

Ensures the commerce_donate line item type contains a donation amount field.

File

./commerce_donate.module, line 61
Module provides donation line item, product and product display node for Drupal Commerce.

Code

function commerce_donate_commerce_donate_line_item_configuration($line_item_type) {
  $type = $line_item_type['type'];
  commerce_product_line_item_configuration($line_item_type);

  // Look for or add a donation amount decimal textfield to the commerce_donate line item type.
  $field_name = 'commerce_donate_amount';
  $field = field_info_field($field_name);
  $instance = field_info_instance('commerce_line_item', $field_name, $type);
  if (empty($field)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'number_decimal',
      'cardinality' => 1,
      'entity_types' => array(
        'commerce_line_item',
      ),
      'translatable' => FALSE,
      'locked' => FALSE,
    );
    $field = field_create_field($field);
  }
  if (empty($instance)) {
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'commerce_line_item',
      'bundle' => $type,
      'label' => t('Amount'),
      'required' => TRUE,
      'commerce_cart_settings' => array(
        'field_access' => 1,
      ),
      'settings' => array(
        'prefix' => '€',
        'min' => 1,
      ),
      'widget' => array(
        'type' => 'select_or_other_buttons',
        'weight' => 0,
        'settings' => array(
          'available_options' => "5|5.00\n10|10.00\n20|20.00\n50|50.00",
          'other' => 'Other amount',
          'other_unknown_defaults' => 'other',
        ),
      ),
      'display' => array(
        'display' => array(
          'label' => 'hidden',
          'weight' => 0,
        ),
      ),
      'default_value' => array(
        0 => array(
          'value' => 5,
        ),
      ),
    );
    field_create_instance($instance);
  }
}