You are here

function commerce_billy_flush_caches in Commerce Billy 7

Implements hook_flush_caches().

File

./commerce_billy.module, line 224
Commerce Billy module.

Code

function commerce_billy_flush_caches() {
  $entity_info = entity_get_info('commerce_order');
  $order_types = array_keys($entity_info['bundles']);

  // Allow modules to alter the list of order types to which invoice fields
  // will be added. Some order types might not need the invoice fields.
  drupal_alter('commerce_billy_order_types', $order_types);
  $field_definition = array(
    'type' => 'datestamp',
    'locked' => FALSE,
    'cardinality' => 1,
    'settings' => array(
      'granularity' => array(
        'year' => 'year',
        'month' => 'month',
        'day' => 'day',
        'hour' => 'hour',
        'minute' => 'minute',
        'second' => 'second',
      ),
    ),
  );
  $instance_definition = array(
    'entity_type' => 'commerce_order',
    'required' => FALSE,
    'settings' => array(
      'default_value' => 'blank',
    ),
    'widget' => array(
      'type' => 'date_text',
      'settings' => array(
        'input_format' => 'd.m.Y - H:i:s',
        'no_fieldset' => 0,
      ),
    ),
  );
  $fields = array(
    'field_commerce_billy_cancel_date' => array(
      'label' => 'Cancel date',
      'description' => 'Internally set once the order is cancelled.',
    ),
    'field_commerce_billy_i_date' => array(
      'label' => 'Invoice date',
      'description' => 'Internally set once the order is invoiced',
    ),
  );
  foreach ($fields as $field_name => $field_details) {
    $field = field_info_field($field_name);
    if (!$field) {
      $field = array(
        'field_name' => $field_name,
      ) + $field_definition;
      field_create_field($field);
    }

    // Ensure each order type has an instance of the field.
    foreach ($order_types as $order_type) {
      $instance = field_info_instance('commerce_order', $field_name, $order_type);
      if (!$instance) {
        $instance = array(
          'field_name' => $field_name,
          'bundle' => $order_type,
          'label' => $field_details['label'],
          'description' => $field_details['description'],
        ) + $instance_definition;
        field_create_instance($instance);
      }
    }
  }
}