verf.module in Views Entity Reference Filter 8
Contains hook implementations.
File
verf.moduleView source
<?php
/**
* @file
* Contains hook implementations.
*/
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
/**
* Implements hook_views_data_alter().
*/
function verf_views_data_alter(array &$data) {
$manager = \Drupal::entityTypeManager();
$field_config_storage = $manager
->getStorage('field_config');
/** @var \Drupal\field\FieldConfigInterface[] $field_configs */
$field_configs = $field_config_storage
->loadByProperties([
'field_type' => 'entity_reference',
]);
foreach ($field_configs as $field_config) {
$table_name = $field_config
->getTargetEntityTypeId() . '__' . $field_config
->getName();
$column_name = $field_config
->getName() . '_target_id';
if (isset($data[$table_name][$column_name])) {
$data[$table_name][$column_name . '_verf'] = [
'title' => t('@label (VERF selector)', [
'@label' => $field_config
->label(),
]),
'group' => $data[$table_name][$column_name]['group'],
'help' => $data[$table_name][$column_name]['help'],
'filter' => [
'field' => $column_name,
'table' => $table_name,
'id' => 'verf',
'additional fields' => [],
'field_name' => $field_config
->getName(),
'entity_type' => $field_config
->getTargetEntityTypeId(),
'verf_target_entity_type_id' => $field_config
->getSetting('target_type'),
'allow empty' => TRUE,
],
];
}
}
foreach ($manager
->getDefinitions() as $entity_type) {
if (!$entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
continue;
}
// We cannot alter Views data if we cannot map fields to tables.
$entity_storage = $manager
->getStorage($entity_type
->id());
if (!$entity_storage instanceof SqlEntityStorageInterface) {
continue;
}
/** @var \Drupal\Core\Field\FieldDefinitionInterface[] $base_field_definitions */
$base_field_definitions = \Drupal::service('entity_field.manager')
->getBaseFieldDefinitions($entity_type
->id());
$table_mapping = $entity_storage
->getTableMapping();
// Loop through all of this entity type's stored fields.
foreach ($table_mapping
->getTableNames() as $table_name) {
foreach ($table_mapping
->getFieldNames($table_name) as $field_name) {
// Skip fields that are not base fields.
if (!isset($base_field_definitions[$field_name])) {
continue;
}
// Only add to entity_reference fields.
$base_field_storage_definition = $base_field_definitions[$field_name]
->getFieldStorageDefinition();
if ($base_field_storage_definition
->getType() !== 'entity_reference') {
continue;
}
$settings = $base_field_storage_definition
->getSettings();
$column_name = $table_mapping
->getFieldColumnName($base_field_storage_definition, $field_name);
$data[$table_name][$column_name . '_verf'] = verf_get_filter($base_field_storage_definition
->getLabel(), $column_name, $table_name, $field_name, $entity_type
->id(), $settings['target_type']);
}
}
}
}
/**
* Gets an array for views.
*/
function verf_get_filter($label, $column_name, $table_name, $field_name, $entity_type_id, $target_entity_type_id, $group = 'verf', $help = '') {
return [
'title' => t('@label (VERF selector)', [
'@label' => $label,
]),
'group' => $group,
'help' => $help,
'filter' => [
'field' => $column_name,
'table' => $table_name,
'id' => 'verf',
'additional fields' => [],
'field_name' => $field_name,
'entity_type' => $entity_type_id,
'verf_target_entity_type_id' => $target_entity_type_id,
'allow empty' => TRUE,
],
];
}
Functions
Name | Description |
---|---|
verf_get_filter | Gets an array for views. |
verf_views_data_alter | Implements hook_views_data_alter(). |