summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/src/Hook/FileViewsHooks.php
blob: cdb764fdba5a8c77a48e4813b6b41cadce42142e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php

declare(strict_types=1);

namespace Drupal\file\Hook;

use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\views\FieldViewsDataProvider;

/**
 * Hook implementations for file.
 */
class FileViewsHooks {

  use StringTranslationTrait;

  public function __construct(
    protected readonly EntityTypeManagerInterface $entityTypeManager,
    protected readonly EntityFieldManagerInterface $entityFieldManager,
    protected readonly ?FieldViewsDataProvider $fieldViewsDataProvider,
  ) {}

  /**
   * Implements hook_field_views_data().
   *
   * Views integration for file fields. Adds a file relationship to the default
   * field data.
   *
   * @see FieldViewsDataProvider::defaultFieldImplementation()
   */
  #[Hook('field_views_data')]
  public function fieldViewsData(FieldStorageConfigInterface $field_storage): array {
    $data = $this->fieldViewsDataProvider->defaultFieldImplementation($field_storage);
    foreach ($data as $table_name => $table_data) {
      // Add the relationship only on the fid field.
      $data[$table_name][$field_storage->getName() . '_target_id']['relationship'] = [
        'id' => 'standard',
        'base' => 'file_managed',
        'entity type' => 'file',
        'base field' => 'fid',
        'label' => $this->t('file from @field_name', [
          '@field_name' => $field_storage->getName(),
        ]),
      ];
    }
    return $data;
  }

  /**
   * Implements hook_field_views_data_views_data_alter().
   *
   * Views integration to provide reverse relationships on file fields.
   */
  #[Hook('field_views_data_views_data_alter')]
  public function fieldViewsDataViewsDataAlter(array &$data, FieldStorageConfigInterface $field_storage): void {
    $entity_type_id = $field_storage->getTargetEntityTypeId();
    $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
    $field_name = $field_storage->getName();
    $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
    /** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
    $table_mapping = $this->entityTypeManager->getStorage($entity_type_id)->getTableMapping();
    [$label] = $this->entityFieldManager->getFieldLabels($entity_type_id, $field_name);
    $data['file_managed'][$pseudo_field_name]['relationship'] = [
      'title' => $this->t('@entity using @field', [
        '@entity' => $entity_type->getLabel(),
        '@field' => $label,
      ]),
      'label' => $this->t('@field_name', [
        '@field_name' => $field_name,
      ]),
      'group' => $entity_type->getLabel(),
      'help' => $this->t('Relate each @entity with a @field set to the file.', [
        '@entity' => $entity_type->getLabel(),
        '@field' => $label,
      ]),
      'id' => 'entity_reverse',
      'base' => $entity_type->getDataTable() ?: $entity_type->getBaseTable(),
      'entity_type' => $entity_type_id,
      'base field' => $entity_type->getKey('id'),
      'field_name' => $field_name,
      'field table' => $table_mapping->getDedicatedDataTableName($field_storage),
      'field field' => $field_name . '_target_id',
      'join_extra' => [
        0 => [
          'field' => 'deleted',
          'value' => 0,
          'numeric' => TRUE,
        ],
      ],
    ];
  }

}