You are here

function commerce_file_views_data_alter in Commerce File 7

Implements hook_views_data_alter().

File

views/commerce_file.views.inc, line 365
Export Drupal Commerce file licenses to Views.

Code

function commerce_file_views_data_alter(&$data) {

  // Product alters
  if (isset($data['commerce_product'])) {

    // Add the product filter for filtering by product types with Commerce File Fields
    $data['commerce_product']['commerce_file_product_type'] = array(
      'title' => t('Product is a product type with Commerce File fields'),
      'help' => t("Filter products to those of a product types that contain Commerce File fields."),
      'filter' => array(
        'handler' => 'commerce_file_handler_filter_file_product_type',
      ),
    );
  }

  // Order alters
  if (isset($data['commerce_order'])) {

    // Define a handler for an area used for order issue licenses.
    $data['commerce_order']['license_issue_form'] = array(
      'title' => t('Issue File Licenses Form'),
      'help' => t('Displays a form to update all file licenses associated with this order.'),
      'area' => array(
        'handler' => 'commerce_file_handler_area_license_issue_order_form',
      ),
    );
  }

  // Line item alters
  if (isset($data['commerce_line_item'])) {

    // Adds a field for a form to issue licenses based on a line item.
    $data['commerce_line_item']['license_issue_form'] = array(
      'field' => array(
        'title' => t('Issue File Licenses Form'),
        'help' => t('Displays a form to update all file licenses associated with this line item.'),
        'handler' => 'commerce_file_handler_field_license_issue_line_item_form',
      ),
    );

    // Line item reference field inverse relationship
    // - Adds inverse relationships between the line items and entity types referencing
    // - Commerce File is one of the few modules to use line item references outside of orders
    foreach (field_info_fields() as $field_name => $field) {
      if ($field['type'] !== 'commerce_line_item_reference') {
        continue;
      }
      foreach ($field['bundles'] as $entity_type => $bundles) {
        if ($entity_type == 'commerce_order') {
          continue;
        }
        $entity_info = entity_get_info($entity_type);
        $pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
        list($label, $all_labels) = field_views_field_label($field['field_name']);
        $entity_label = $entity_info['label'];
        if ($entity_label == t('Node')) {
          $entity_label = t('Content');
        }
        if (!isset($data['commerce_line_item'][$pseudo_field_name]['relationship'])) {
          $data['commerce_line_item'][$pseudo_field_name]['relationship'] = array(
            'title' => t('@entity using @field', array(
              '@entity' => $entity_label,
              '@field' => $label,
            )),
            'help' => t('Relate each @entity with a @field set to the line item.', array(
              '@entity' => $entity_label,
              '@field' => $label,
            )),
            'handler' => 'views_handler_relationship_entity_reverse',
            'field_name' => $field['field_name'],
            'field table' => _field_sql_storage_tablename($field),
            'field field' => $field['field_name'] . '_line_item_id',
            'base' => $entity_info['base table'],
            'base field' => $entity_info['entity keys']['id'],
            'label' => t('!field_name', array(
              '!field_name' => $field['field_name'],
            )),
            'join_extra' => array(
              0 => array(
                'field' => 'entity_type',
                'value' => $entity_type,
              ),
              1 => array(
                'field' => 'deleted',
                'value' => 0,
                'numeric' => TRUE,
              ),
            ),
          );
        }
      }
    }
  }
}