You are here

function entity_views_field_definition in Entity API 7

Helper function for adding a Views field definition to data selection based Views tables.

Parameters

$field: The data selector of the field to add. E.g. "title" would derive the node title property, "body:summary" the node body's summary.

array $property_info: The property information for which to create a field definition.

array $table: The table into which the definition should be inserted.

$title_prefix: Internal use only.

See also

entity_views_table_definition()

2 calls to entity_views_field_definition()
entity_views_data in views/entity.views.inc
Implements hook_views_data().
entity_views_table_definition in views/entity.views.inc
Helper function for getting data selection based entity Views table definitions.

File

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

Code

function entity_views_field_definition($field, array $property_info, array &$table, $title_prefix = '') {
  $additional = array();
  $additional_field = array();

  // Create a valid Views field identifier (no colons, etc.). Keep the original
  // data selector as real field though.
  $key = _entity_views_field_identifier($field, $table);
  if ($key != $field) {
    $additional['real field'] = $field;
  }
  $field_name = EntityFieldHandlerHelper::get_selector_field_name($field);
  $field_handlers = entity_views_get_field_handlers();
  $property_info += entity_property_info_defaults();
  $type = entity_property_extract_innermost_type($property_info['type']);
  $title = $title_prefix . $property_info['label'];
  if ($info = entity_get_info($type)) {
    $additional['relationship'] = array(
      'handler' => $field_handlers['relationship'],
      'base' => 'entity_' . $type,
      'base field' => $info['entity keys']['id'],
      'relationship field' => $field,
      'label' => $title,
    );
    if ($property_info['type'] != $type) {

      // This is a list of entities, so we should mark the relationship as such.
      $additional['relationship']['multiple'] = TRUE;
    }

    // Implementers of the field handlers alter hook could add handlers for
    // specific entity types.
    if (!isset($field_handlers[$type])) {
      $type = 'entity';
    }
  }
  elseif (!empty($property_info['field'])) {
    $type = 'field';

    // Views' Field API field handler needs some extra definitions to work.
    $additional_field['field_name'] = $field_name;
    $additional_field['entity_tables'] = array();
    $additional_field['entity type'] = $table['table']['entity type'];
    $additional_field['is revision'] = FALSE;
  }
  elseif (isset($property_info['options list']) && is_callable($property_info['options list'])) {

    // If this is a nested property, we need to get rid of all prefixes first.
    $type = 'options';
    $additional_field['options callback'] = array(
      'function' => $property_info['options list'],
      'info' => $property_info,
    );
  }
  elseif ($type == 'decimal') {
    $additional_field['float'] = TRUE;
  }
  if (isset($field_handlers[$type])) {
    $table += array(
      $key => array(),
    );
    $table[$key] += array(
      'title' => $title,
      'help' => empty($property_info['description']) ? t('(No information available)') : $property_info['description'],
      'field' => array(),
    );
    $table[$key]['field'] += array(
      'handler' => $field_handlers[$type],
      'type' => $property_info['type'],
    );
    $table[$key] += $additional;
    $table[$key]['field'] += $additional_field;
  }
  if (!empty($property_info['property info'])) {
    foreach ($property_info['property info'] as $nested_key => $nested_property) {
      entity_views_field_definition($field . ':' . $nested_key, $nested_property, $table, $title . ' » ');
    }
  }
}