You are here

ref_field.views.inc in (Entity)Reference Field Synchronization 7

File

includes/views/ref_field.views.inc
View source
<?php

/**
 * based on code from Entity Reference by Damien Tournoud
 * http://drupal.org/project/entityreference
 * http://drupal.org/user/22211
 */

/**
 * Implements hook_field_views_data().
 */
function ref_field_field_views_data($field) {

  // Get default views field values
  $data = field_views_field_default_views_data($field);

  // Get references entity info
  $entity_info = entity_get_info($field['settings']['entity']);
  foreach ($data as $table_name => $table_data) {

    // Check if base tables is set
    // Is this needed? Don't they all have one?
    if (isset($entity_info['base table'])) {
      $entity = $entity_info['label'];

      // If a node, name it as Content
      // Consistent UI (?)
      if ($entity == t('Node')) {
        $entity = t('Content');
      }

      // Add relationship to the field
      $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;
}

/**
 * Implements hook_field_views_data_views_data_alter().
 */
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']);

    // Check if base tables is set
    // Is this needed? Don't they all have one?
    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 a node, name it as Content
      // Consistent UI (?)
      if ($entity == t('Node')) {
        $entity = t('Content');
      }
      if ($target_entity == t('Node')) {
        $target_entity = t('Content');
      }

      // Add back-reference relationship to the field
      $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'],
        )),
      );
    }
  }
}