function ImageViewsHooks::fieldViewsData

Implements hook_field_views_data().

Views integration for image fields. Adds an image relationship to the default field data.

Attributes

#[Hook('field_views_data')]

See also

FieldViewsDataProvider::defaultFieldImplementation()

File

core/modules/image/src/Hook/ImageViewsHooks.php, line 29

Class

ImageViewsHooks
Hook implementations for image.

Namespace

Drupal\image\Hook

Code

public function fieldViewsData(FieldStorageConfigInterface $field_storage) : array {
  $data = \Drupal::service('views.field_data_provider')->defaultFieldImplementation($field_storage);
  foreach ($data as $table_name => $table_data) {
    // Add the relationship only on the target_id field.
    $data[$table_name][$field_storage->getName() . '_target_id']['relationship'] = [
      'id' => 'standard',
      'base' => 'file_managed',
      'entity type' => 'file',
      'base field' => 'fid',
      'label' => $this->t('image from @field_name', [
        '@field_name' => $field_storage->getName(),
      ]),
    ];
  }
  return $data;
}

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