2015-08-18 00:00:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Provide views data for image.module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use Drupal\field\FieldStorageConfigInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_field_views_data().
|
|
|
|
*
|
|
|
|
* Views integration for image fields. Adds an image relationship to the default
|
|
|
|
* field data.
|
|
|
|
*
|
|
|
|
* @see views_field_default_views_data()
|
|
|
|
*/
|
|
|
|
function image_field_views_data(FieldStorageConfigInterface $field_storage) {
|
|
|
|
$data = views_field_default_views_data($field_storage);
|
|
|
|
foreach ($data as $table_name => $table_data) {
|
|
|
|
// Add the relationship only on the target_id field.
|
2017-04-13 14:53:35 +00:00
|
|
|
$data[$table_name][$field_storage->getName() . '_target_id']['relationship'] = [
|
2015-08-18 00:00:26 +00:00
|
|
|
'id' => 'standard',
|
|
|
|
'base' => 'file_managed',
|
|
|
|
'entity type' => 'file',
|
|
|
|
'base field' => 'fid',
|
2017-04-13 14:53:35 +00:00
|
|
|
'label' => t('image from @field_name', ['@field_name' => $field_storage->getName()]),
|
|
|
|
];
|
2015-08-18 00:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_field_views_data_views_data_alter().
|
|
|
|
*
|
|
|
|
* Views integration to provide reverse relationships on image fields.
|
|
|
|
*/
|
|
|
|
function image_field_views_data_views_data_alter(array &$data, FieldStorageConfigInterface $field_storage) {
|
|
|
|
$entity_type_id = $field_storage->getTargetEntityTypeId();
|
|
|
|
$field_name = $field_storage->getName();
|
|
|
|
$entity_manager = \Drupal::entityManager();
|
|
|
|
$entity_type = $entity_manager->getDefinition($entity_type_id);
|
|
|
|
$pseudo_field_name = 'reverse_' . $field_name . '_' . $entity_type_id;
|
|
|
|
/** @var \Drupal\Core\Entity\Sql\DefaultTableMapping $table_mapping */
|
|
|
|
$table_mapping = $entity_manager->getStorage($entity_type_id)->getTableMapping();
|
|
|
|
|
|
|
|
list($label) = views_entity_field_label($entity_type_id, $field_name);
|
|
|
|
|
2017-04-13 14:53:35 +00:00
|
|
|
$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]),
|
|
|
|
'help' => t('Relate each @entity with a @field set to the image.', ['@entity' => $entity_type->getLabel(), '@field' => $label]),
|
2015-08-18 00:00:26 +00:00
|
|
|
'group' => $entity_type->getLabel(),
|
|
|
|
'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',
|
2017-04-13 14:53:35 +00:00
|
|
|
'join_extra' => [
|
|
|
|
0 => [
|
2015-08-18 00:00:26 +00:00
|
|
|
'field' => 'deleted',
|
|
|
|
'value' => 0,
|
|
|
|
'numeric' => TRUE,
|
2017-04-13 14:53:35 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2015-08-18 00:00:26 +00:00
|
|
|
}
|