Same name and namespace in other branches
  1. 8.9.x core/modules/views/views.api.php \hook_field_views_data_views_data_alter()
  2. 9 core/modules/views/views.api.php \hook_field_views_data_views_data_alter()

Alter the Views data on a per field basis.

The Views module's implementation of hook_views_data_alter() invokes this for each field storage, in the module that defines the field type. It is not invoked in other modules.

Unlike hook_field_views_data_alter(), this operates on the whole of the views data. This allows a field type to add data that concerns its fields in other tables, which would not yet be defined at the point when hook_field_views_data() and hook_field_views_data_alter() are invoked. For example, entity_reference adds reverse relationships on the tables for the entities which are referenced by entity_reference fields.

(Note: this is weirdly named so as not to conflict with hook_field_views_data_alter().)

Parameters

array $data: The views data.

\Drupal\field\FieldStorageConfigInterface $field: The field storage config entity.

See also

hook_field_views_data()

hook_field_views_data_alter()

views_views_data_alter()

Related topics

2 functions implement hook_field_views_data_views_data_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

file_field_views_data_views_data_alter in core/modules/file/file.views.inc
Implements hook_field_views_data_views_data_alter().
image_field_views_data_views_data_alter in core/modules/image/image.views.inc
Implements hook_field_views_data_views_data_alter().

File

core/modules/views/views.api.php, line 632
Describes hooks and plugins provided by the Views module.

Code

function hook_field_views_data_views_data_alter(array &$data, \Drupal\field\FieldStorageConfigInterface $field) {
  $field_name = $field
    ->getName();
  $data_key = 'field_data_' . $field_name;
  $entity_type_id = $field
    ->getTargetEntityTypeId();
  $entity_type = \Drupal::entityTypeManager()
    ->getDefinition($entity_type_id);
  $pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
  [
    $label,
  ] = views_entity_field_label($entity_type_id, $field_name);
  $table_mapping = \Drupal::entityTypeManager()
    ->getStorage($entity_type_id)
    ->getTableMapping();

  // Views data for this field is in $data[$data_key].
  $data[$data_key][$pseudo_field_name]['relationship'] = [
    'title' => t('@entity using @field', [
      '@entity' => $entity_type
        ->getLabel(),
      '@field' => $label,
    ]),
    'help' => t('Relate each @entity with a @field set to the term.', [
      '@entity' => $entity_type
        ->getLabel(),
      '@field' => $label,
    ]),
    'id' => 'entity_reverse',
    'field_name' => $field_name,
    'entity_type' => $entity_type_id,
    'field table' => $table_mapping
      ->getDedicatedDataTableName($field),
    'field field' => $field_name . '_target_id',
    'base' => $entity_type
      ->getBaseTable(),
    'base field' => $entity_type
      ->getKey('id'),
    'label' => $field_name,
    'join_extra' => [
      0 => [
        'field' => 'deleted',
        'value' => 0,
        'numeric' => TRUE,
      ],
    ],
  ];
}