5 * Provides views data for the entity_reference_revisions module.
8 use Drupal\field\FieldStorageConfigInterface;
11 * Implements hook_field_views_data().
13 function entity_reference_revisions_field_views_data(FieldStorageConfigInterface $field_storage) {
14 $data = views_field_default_views_data($field_storage);
15 $entity_manager = \Drupal::entityTypeManager();
16 foreach ($data as $table_name => $table_data) {
17 // Add a relationship to the target entity type.
18 $target_entity_type_id = $field_storage->getSetting('target_type');
19 $target_entity_type = $entity_manager->getDefinition($target_entity_type_id);
20 $entity_type_id = $field_storage->getTargetEntityTypeId();
21 $entity_type = $entity_manager->getDefinition($entity_type_id);
22 $target_base_table = $target_entity_type->getDataTable() ?: $target_entity_type->getBaseTable();
23 $field_name = $field_storage->getName();
25 // Provide a relationship for the entity type with the entity reference
28 '@label' => $target_entity_type->getLabel(),
29 '@field_name' => $field_name,
31 $data[$table_name][$field_name]['relationship'] = array(
32 'title' => t('@label referenced from @field_name', $args),
33 'label' => t('@field_name: @label', $args),
34 'group' => $entity_type->getLabel(),
35 'help' => t('Appears in: @bundles.', array('@bundles' => implode(', ', $field_storage->getBundles()))),
37 'base' => $target_base_table,
38 'entity type' => $target_entity_type_id,
39 'base field' => $target_entity_type->getKey('revision'),
40 'relationship field' => $field_name . '_target_revision_id',
43 // Provide a reverse relationship for the entity type that is referenced by
45 $args['@entity'] = $entity_type->getLabel();
46 $args['@label'] = $target_entity_type->getLowercaseLabel();
47 $pseudo_field_name = 'reverse__' . $entity_type_id . '__' . $field_name;
48 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
49 $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
50 $data[$target_base_table][$pseudo_field_name]['relationship'] = array(
51 'title' => t('@entity using @field_name', $args),
52 'label' => t('@field_name', array('@field_name' => $field_name)),
53 'group' => $target_entity_type->getLabel(),
54 'help' => t('Relate each @entity with a @field_name set to the @label.', $args),
55 'id' => 'entity_reverse',
56 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
57 'entity_type' => $entity_type_id,
58 'base field' => $entity_type->getKey('revision'),
59 'field_name' => $field_name,
60 'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
61 'field field' => $field_name . '_target_revision_id',
62 'join_extra' => array(