Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Provide views data for image.module.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\field\FieldStorageConfigInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Implements hook_field_views_data().
|
Chris@0
|
12 *
|
Chris@0
|
13 * Views integration for image fields. Adds an image relationship to the default
|
Chris@0
|
14 * field data.
|
Chris@0
|
15 *
|
Chris@0
|
16 * @see views_field_default_views_data()
|
Chris@0
|
17 */
|
Chris@0
|
18 function image_field_views_data(FieldStorageConfigInterface $field_storage) {
|
Chris@0
|
19 $data = views_field_default_views_data($field_storage);
|
Chris@0
|
20 foreach ($data as $table_name => $table_data) {
|
Chris@0
|
21 // Add the relationship only on the target_id field.
|
Chris@0
|
22 $data[$table_name][$field_storage->getName() . '_target_id']['relationship'] = [
|
Chris@0
|
23 'id' => 'standard',
|
Chris@0
|
24 'base' => 'file_managed',
|
Chris@0
|
25 'entity type' => 'file',
|
Chris@0
|
26 'base field' => 'fid',
|
Chris@0
|
27 'label' => t('image from @field_name', ['@field_name' => $field_storage->getName()]),
|
Chris@0
|
28 ];
|
Chris@0
|
29 }
|
Chris@0
|
30
|
Chris@0
|
31 return $data;
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Implements hook_field_views_data_views_data_alter().
|
Chris@0
|
36 *
|
Chris@0
|
37 * Views integration to provide reverse relationships on image fields.
|
Chris@0
|
38 */
|
Chris@0
|
39 function image_field_views_data_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) {
|
Chris@0
|
40 $entity_type_id = $field_storage->getTargetEntityTypeId();
|
Chris@0
|
41 $field_name = $field_storage->getName();
|
Chris@0
|
42 $entity_manager = \Drupal::entityManager();
|
Chris@0
|
43 $entity_type = $entity_manager->getDefinition($entity_type_id);
|
Chris@0
|
44 $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
|
Chris@0
|
45 /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
Chris@0
|
46 $table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
|
Chris@0
|
47
|
Chris@0
|
48 list($label) = views_entity_field_label($entity_type_id, $field_name);
|
Chris@0
|
49
|
Chris@0
|
50 $data['file_managed'][$pseudo_field_name]['relationship'] = [
|
Chris@0
|
51 'title' => t('@entity using @field', ['@entity' => $entity_type->getLabel(), '@field' => $label]),
|
Chris@0
|
52 'label' => t('@field_name', ['@field_name' => $field_name]),
|
Chris@0
|
53 'help' => t('Relate each @entity with a @field set to the image.', ['@entity' => $entity_type->getLabel(), '@field' => $label]),
|
Chris@0
|
54 'group' => $entity_type->getLabel(),
|
Chris@0
|
55 'id' => 'entity_reverse',
|
Chris@0
|
56 'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
|
Chris@0
|
57 'entity_type' => $entity_type_id,
|
Chris@0
|
58 'base field' => $entity_type->getKey('id'),
|
Chris@0
|
59 'field_name' => $field_name,
|
Chris@0
|
60 'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
|
Chris@0
|
61 'field field' => $field_name . '_target_id',
|
Chris@0
|
62 'join_extra' => [
|
Chris@0
|
63 0 => [
|
Chris@0
|
64 'field' => 'deleted',
|
Chris@0
|
65 'value' => 0,
|
Chris@0
|
66 'numeric' => TRUE,
|
Chris@0
|
67 ],
|
Chris@0
|
68 ],
|
Chris@0
|
69 ];
|
Chris@0
|
70 }
|