View source
<?php
function ref_field_field_views_data($field) {
$data = field_views_field_default_views_data($field);
$entity_info = entity_get_info($field['settings']['entity']);
foreach ($data as $table_name => $table_data) {
if (isset($entity_info['base table'])) {
$entity = $entity_info['label'];
if ($entity == t('Node')) {
$entity = t('Content');
}
$field_name = $field['field_name'] . '_eid';
$data[$table_name][$field_name]['relationship'] = array(
'handler' => 'views_handler_relationship',
'base' => $entity_info['base table'],
'base field' => $entity_info['entity keys']['id'],
'label' => t('@entity entity referenced from !field_name', array(
'@entity' => $entity,
'!field_name' => $field['field_name'],
)),
'group' => t('Entity Reference'),
'title' => t('Referenced @entity', array(
'@entity' => $entity,
)),
'help' => t('Relate any @entity referenced via !field_name. This relationship will cause duplicated records if there is more than one @entity referenced.', array(
'@entity' => $entity,
'!field_name' => $field['field_name'],
)),
);
}
}
return $data;
}
function ref_field_field_views_data_views_data_alter(&$data, $field) {
foreach ($field['bundles'] as $entity_type => $bundles) {
$target_entity_info = entity_get_info($field['settings']['entity']);
if (isset($target_entity_info['base table'])) {
$entity_info = entity_get_info($entity_type);
$entity = $entity_info['label'];
$target_entity = $target_entity_info['label'];
if ($entity == t('Node')) {
$entity = t('Content');
}
if ($target_entity == t('Node')) {
$target_entity = t('Content');
}
$pseudo_field_name = 'reverse_' . $field['field_name'] . '_' . $entity_type;
$data[$target_entity_info['base table']][$pseudo_field_name]['relationship'] = array(
'handler' => 'views_handler_relationship_entity_reverse',
'field_name' => $field['field_name'],
'field table' => _field_sql_storage_tablename($field),
'field field' => $field['field_name'] . '_eid',
'base' => $entity_info['base table'],
'base field' => $entity_info['entity keys']['id'],
'label' => t('@entity referencing @target_entity from !field_name', array(
'@entity' => $entity,
'@target_entity' => $target_entity,
'!field_name' => $field['field_name'],
)),
'group' => t('Entity reference'),
'title' => t('Referencing @entity', array(
'@entity' => $entity,
)),
'help' => t('Relate any @entity that references back to the listed @target_entity via !field_name. This relationship will cause duplicated records if there is more than one @target_entity referenced on the related @entity.', array(
'@entity' => $entity,
'@target_entity' => $target_entity,
'!field_name' => $field['field_name'],
)),
);
}
}
}