function file_field_views_data_views_data_alter

Same name in other branches
  1. 9 core/modules/file/file.views.inc \file_field_views_data_views_data_alter()
  2. 8.9.x core/modules/file/file.views.inc \file_field_views_data_views_data_alter()
  3. 11.x core/modules/file/file.views.inc \file_field_views_data_views_data_alter()

Implements hook_field_views_data_views_data_alter().

Views integration to provide reverse relationships on file fields.

File

core/modules/file/file.views.inc, line 39

Code

function file_field_views_data_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) {
    $entity_type_id = $field_storage->getTargetEntityTypeId();
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager->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 = $entity_type_manager->getStorage($entity_type_id)
        ->getTableMapping();
    [
        $label,
    ] = views_entity_field_label($entity_type_id, $field_name);
    $data['file_managed'][$pseudo_field_name]['relationship'] = [
        'title' => t('@entity using @field', [
            '@entity' => $entity_type->getLabel(),
            '@field' => $label,
        ]),
        'label' => t('@field_name', [
            '@field_name' => $field_name,
        ]),
        'group' => $entity_type->getLabel(),
        'help' => 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,
            ],
        ],
    ];
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.