You are here

function entity_views_data in Entity API 7

Same name and namespace in other branches
  1. 8 entity.views.inc \entity_views_data()
  2. 8.0 entity.views.inc \entity_views_data()

Implements hook_views_data().

Provides Views integration for entities if they satisfy one of these conditions:

See also

entity_crud_hook_entity_info()

entity_views_table_definition()

File

views/entity.views.inc, line 20
Provide views data for modules making use of the entity CRUD API.

Code

function entity_views_data() {
  $data = array();
  foreach (entity_crud_get_info() as $type => $info) {

    // Provide default integration with the basic controller class if we know
    // the module providing the entity and it does not provide views integration.
    if (!isset($info['views controller class'])) {
      $info['views controller class'] = isset($info['module']) && !module_hook($info['module'], 'views_data') ? 'EntityDefaultViewsController' : FALSE;
    }
    if ($info['views controller class']) {
      $controller = new $info['views controller class']($type);

      // Relationship data may return views data for already existing tables,
      // so merge results on the second level.
      foreach ($controller
        ->views_data() as $table => $table_data) {
        $data += array(
          $table => array(),
        );
        $data[$table] = array_merge($data[$table], $table_data);
      }
    }
  }

  // Add tables based upon data selection "queries" for all entity types.
  foreach (entity_get_info() as $type => $info) {
    $table = entity_views_table_definition($type);
    if ($table) {
      $data['entity_' . $type] = $table;
    }

    // Generally expose properties marked as 'entity views field'.
    $data['views_entity_' . $type] = array();
    foreach (entity_get_all_property_info($type) as $key => $property) {
      if (!empty($property['entity views field'])) {
        entity_views_field_definition($key, $property, $data['views_entity_' . $type]);
      }
    }
  }

  // Expose generally usable entity-related fields.
  foreach (entity_get_info() as $entity_type => $info) {
    if (entity_type_supports($entity_type, 'view')) {

      // Expose a field allowing to display the rendered entity.
      $data['views_entity_' . $entity_type]['rendered_entity'] = array(
        'title' => t('Rendered @entity-type', array(
          '@entity-type' => $info['label'],
        )),
        'help' => t('The @entity-type of the current relationship rendered using a view mode.', array(
          '@entity-type' => $info['label'],
        )),
        'field' => array(
          'handler' => 'entity_views_handler_field_entity',
          'type' => $entity_type,
          // The EntityFieldHandlerHelper treats the 'entity object' data
          // selector as special case for loading the base entity.
          'real field' => 'entity object',
        ),
      );
    }
  }
  $data['entity__global']['table']['group'] = t('Entity');
  $data['entity__global']['table']['join'] = array(
    // #global let's it appear all the time.
    '#global' => array(),
  );
  $data['entity__global']['entity'] = array(
    'title' => t('Rendered entity'),
    'help' => t('Displays a single chosen entity.'),
    'area' => array(
      'handler' => 'entity_views_handler_area_entity',
    ),
  );
  return $data;
}