View source
<?php
namespace Drupal\plugin;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\field\FieldStorageConfigInterface;
use Drupal\plugin\PluginType\PluginTypeManagerInterface;
class ViewsData {
protected $entityFieldManager;
protected $entityTypeManager;
protected $pluginTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, PluginTypeManagerInterface $plugin_type_manager) {
$this->entityFieldManager = $entity_field_manager;
$this->entityTypeManager = $entity_type_manager;
$this->pluginTypeManager = $plugin_type_manager;
}
public function alterViewsData(array &$data) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type) {
if (!$entity_type
->entityClassImplements(FieldableEntityInterface::class)) {
continue;
}
$base_field_definitions = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type
->id());
$entity_storage = $this->entityTypeManager
->getStorage($entity_type
->id());
if (!$entity_storage instanceof SqlEntityStorageInterface) {
continue;
}
$table_mapping = $entity_storage
->getTableMapping();
foreach ($table_mapping
->getTableNames() as $table_name) {
foreach ($table_mapping
->getFieldNames($table_name) as $field_name) {
if (!isset($base_field_definitions[$field_name])) {
continue;
}
$base_field_storage_definition = $base_field_definitions[$field_name]
->getFieldStorageDefinition();
if (strpos($base_field_storage_definition
->getType(), 'plugin:') !== 0) {
continue;
}
if ($base_field_storage_definition
->getCardinality() == 1) {
$plugin_id_column_name = $table_mapping
->getFieldColumnName($base_field_storage_definition, 'plugin_id');
$plugin_configuration_column_name = $table_mapping
->getFieldColumnName($base_field_storage_definition, 'plugin_configuration');
}
else {
$plugin_id_column_name = $base_field_storage_definition
->getName() . '__plugin_id';
$plugin_configuration_column_name = $base_field_storage_definition
->getName() . '__plugin_configuration';
}
$this
->alterPluginFieldData($data, $base_field_storage_definition, $table_name, $plugin_id_column_name, $plugin_configuration_column_name);
}
}
}
}
public function alterFieldViewsData(array &$data, FieldStorageConfigInterface $field_storage) {
if (strpos($field_storage
->getType(), 'plugin:') === 0) {
$table_name = $field_storage
->getTargetEntityTypeId() . '__' . $field_storage
->getName();
$plugin_id_column_name = $field_storage
->getName() . '_plugin_id';
$plugin_configuration_column_name = $field_storage
->getName() . '_plugin_configuration';
if (!isset($data[$table_name][$plugin_id_column_name])) {
return;
}
$this
->alterPluginFieldData($data, $field_storage, $table_name, $plugin_id_column_name, $plugin_configuration_column_name);
}
}
protected function alterPluginFieldData(array &$data, FieldStorageDefinitionInterface $field_storage_definition, $table_name, $plugin_id_column_name, $plugin_configuration_column_name = NULL) {
if (strpos($field_storage_definition
->getType(), 'plugin:')) {
throw new \InvalidArgumentException('The Views data being altered is not for a "plugin" field.');
}
if (!isset($data[$table_name][$plugin_id_column_name])) {
return;
}
$plugin_type_id = substr($field_storage_definition
->getType(), 7);
$plugin_type = $this->pluginTypeManager
->getPluginType($plugin_type_id);
$plugin_id_old_title = $data[$table_name][$plugin_id_column_name]['title'];
$plugin_id_title_arguments = [
'@type_label' => $plugin_type
->getLabel(),
'@name' => $field_storage_definition
->getName(),
'@column' => $plugin_id_column_name,
];
$data[$table_name][$plugin_id_column_name]['title'] = new TranslatableMarkup('@type_label ID (@name:@column)', $plugin_id_title_arguments, $plugin_id_old_title
->getOptions());
$data[$table_name][$plugin_id_column_name]['filter']['id'] = 'plugin_id';
$data[$table_name][$plugin_id_column_name]['filter']['plugin_type_id'] = $plugin_type_id;
if ($plugin_configuration_column_name) {
unset($data[$table_name][$plugin_configuration_column_name]);
}
}
}