Views integration

Last updated on
19 January 2017

Entity API tries to generate suitable Views integration for the entity based upon:

  • The schema information of its base table and the provided entity
  • Property information.

For the integration to work, you need to:

  • Make use of the Entity CRUD API as described here: http://drupal.org/node/878804. Most important is to provide the 'EntityAPIController' or descending class as the controller class for the entity type.
  • Provide the name of your module in your hook_entity_info():
/**
 * Implements hook_entity_info().
 */
function entity_test_entity_info() {
  return array(
    'entity_test' => array(
      'label' => t('Test Entity'),
      // Entity API needs to know about your module for the views integration to work
      'module' => 'entity_test',
     ..

While Entity API tries to provide Views the needed information based on your schema, on some cases it may fail or not give the results you expected. To manually expose your data to Views as fields, filters or arguments, you'll have to configure this in hook_entity_property_info() by making use of 'schema field'. This mapping overrides Entity API's 'guessing' and exposes those properties as Views fields, filters and arguments the way you need them to.

/**
 * Implements hook_entity_property_info().
 */
function entity_test_entity_property_info() {
  $info = array();
  $properties = &$info['entity_test']['properties'];

  $properties['some_field'] = array(
    'type'  => 'integer',
    'label' => t('Some field'),
    'description' => t('This is some field.'),

    // Map here the field from hook_schema().
    'schema field' => 'some_field',
  );
  // Other fields...
  return $info;
}

Note that if you use this, you need to define ALL fields since it completely overrides Entity API's implementation. If you just need to alter part of your fields, you can use hook_entity_property_info_alter() in similar manner.

/**
 * Implements hook_entity_property_info_alter().
 */
function entity_test_entity_property_info_alter(&$info) {
  $properties = &$info['entity_test']['properties'];

  $properties['some_field'] = array(
    'type'  => 'integer',
    'label' => t('Some field'),
    'description' => t('This is some field.'),

    // Map here the field from hook_schema().
    'schema field' => 'some_field',
  );
  // Other fields...
}

Another way to alter this data is to extend EntityDefaultViewsController with your own implementation.

First let Entity API know you will use a custom Views Controller:

/**
 * Implements hook_entity_info().
 */
function entity_test_entity_info() {
  return array(
    'entity_test' => array(
      'label' => t('Test Entity'),
      // Entity API needs to know about your module for the views integration to work
      'module' => 'entity_test',
      'views controller class' => 'EntityTestViewsController',
     ..

Then implement the class:

/**
 * EntityTest Views Controller class.
 */
class EntityTestViewsController extends EntityDefaultViewsController {

  /**
   * Edit or add extra fields to views_data().
   */
  public function views_data() {
    $data = parent::views_data();

    // Add your custom data here

    /* 
    * Example: change the handler of a field
    * if the 'created' field is a unix timestamp in the database, 
    * Entity API will set the handler to views_handler_field_numeric, 
    * change this to the views date handler
    */ 
    $data['entity_test']['created']['field']['handler'] = 'views_handler_field_date';

    return $data;
  }
}

The above technique is preferred in some cases since it does not involve using any Views hooks directly.