You are here

function commerce_product_reference_entity_info_alter in Commerce Core 7

Implements hook_entity_info_alter().

Adds the line item and the product display-specific view modes to the product.

File

modules/product_reference/commerce_product_reference.module, line 1137
Defines a field type for referencing products from other entities.

Code

function commerce_product_reference_entity_info_alter(&$entity_info) {
  $entity_info['commerce_product']['view modes']['line_item'] = array(
    'label' => t('Line item'),
    'custom settings' => TRUE,
  );

  // Query the field tables directly to avoid creating a loop in the Field API:
  // it is not legal to call any of the field_info_*() in
  // hook_entity_info(), as field_read_instances() calls entity_get_info().
  $query = db_select('field_config_instance', 'fci', array(
    'fetch' => PDO::FETCH_ASSOC,
  ));
  $query
    ->join('field_config', 'fc', 'fc.id = fci.field_id');
  $query
    ->fields('fci', array(
    'entity_type',
  ));
  $query
    ->condition('fc.type', 'commerce_product_reference');
  $query
    ->condition('fc.deleted', 0);
  $query
    ->distinct();
  foreach ($query
    ->execute() as $instance) {
    $entity_type = $instance['entity_type'];
    if (!empty($entity_info[$entity_type]['view modes'])) {
      foreach ($entity_info[$entity_type]['view modes'] as $view_mode => $view_mode_info) {
        $entity_info['commerce_product']['view modes'][$entity_type . '_' . $view_mode] = array(
          'label' => t('@entity_type: @view_mode', array(
            '@entity_type' => $entity_info[$entity_type]['label'],
            '@view_mode' => $view_mode_info['label'],
          )),
          // UX: Enable the 'Node: teaser' mode by default, if present.
          'custom settings' => $entity_type == 'node' && $view_mode == 'teaser',
        );
      }
    }
  }
}