FieldStorageSubscriber.php in Dynamic Entity Reference 8.2
File
src/EventSubscriber/FieldStorageSubscriber.php
View source
<?php
namespace Drupal\dynamic_entity_reference\EventSubscriber;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeEvent;
use Drupal\Core\Entity\EntityTypeEvents;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageException;
use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Field\FieldStorageDefinitionEvent;
use Drupal\Core\Field\FieldStorageDefinitionEvents;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\dynamic_entity_reference\Storage\IntColumnHandlerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FieldStorageSubscriber implements EventSubscriberInterface {
protected $entityTypeManager;
protected $intColumnHandler;
protected $entityFieldManager;
protected $connection;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, IntColumnHandlerInterface $int_column_handler, Connection $connection) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
$this->intColumnHandler = $int_column_handler;
$this->connection = $connection;
}
public static function getSubscribedEvents() {
$events[FieldStorageDefinitionEvents::CREATE][] = [
'onFieldStorage',
100,
];
$events[EntityTypeEvents::CREATE][] = [
'onEntityType',
100,
];
return $events;
}
public function onFieldStorage(FieldStorageDefinitionEvent $event) {
$definition = $event
->getFieldStorageDefinition();
$this
->handleEntityType($definition
->getTargetEntityTypeId(), $definition);
}
public function onEntityType(EntityTypeEvent $event) {
$this
->handleEntityType($event
->getEntityType()
->id());
}
public function handleEntityType($entity_type_id, FieldStorageDefinitionInterface $field_storage_definition = NULL) {
$storage = $this->entityTypeManager
->getStorage($entity_type_id);
$der_fields = $this->entityFieldManager
->getFieldMapByFieldType('dynamic_entity_reference');
if ($field_storage_definition && $field_storage_definition
->getType() === 'dynamic_entity_reference') {
$der_fields[$entity_type_id][$field_storage_definition
->getName()] = TRUE;
}
$entity_type = $this->entityTypeManager
->getDefinition($entity_type_id);
$tables = [];
$index_columns = [];
if ($storage instanceof SqlEntityStorageInterface && !empty($der_fields[$entity_type_id])) {
$storage_definitions = $this->entityFieldManager
->getActiveFieldStorageDefinitions($entity_type_id);
if ($field_storage_definition) {
$storage_definitions[$field_storage_definition
->getName()] = $field_storage_definition;
}
$mapping = $storage
->getTableMapping($storage_definitions);
foreach (array_keys($der_fields[$entity_type_id]) as $field_name) {
try {
$table = $mapping
->getFieldTableName($field_name);
$column = $mapping
->getFieldColumnName($storage_definitions[$field_name], 'target_id');
$index_column = $mapping
->getFieldColumnName($storage_definitions[$field_name], 'target_type');
} catch (SqlContentEntityStorageException $e) {
continue;
}
$tables[$table][] = $column;
$schema_info = $storage_definitions[$field_name]
->getSchema();
$index_columns[$table] = [
$index_column => $schema_info['columns']['target_type'],
];
if ($entity_type
->isRevisionable() && $storage_definitions[$field_name]
->isRevisionable()) {
try {
if ($mapping
->requiresDedicatedTableStorage($storage_definitions[$field_name])) {
$tables[$mapping
->getDedicatedRevisionTableName($storage_definitions[$field_name])][] = $column;
$index_columns[$mapping
->getDedicatedRevisionTableName($storage_definitions[$field_name])] = [
$index_column => $schema_info['columns']['target_type'],
];
}
elseif ($mapping
->allowsSharedTableStorage($storage_definitions[$field_name])) {
$revision_table = $entity_type
->getRevisionDataTable() ?: $entity_type
->getRevisionTable();
$tables[$revision_table][] = $column;
$tables[$revision_table] = array_unique($tables[$revision_table]);
$index_columns[$revision_table] = [
$index_column => $schema_info['columns']['target_type'],
];
}
} catch (SqlContentEntityStorageException $e) {
}
}
}
$new = [];
foreach ($tables as $table => $columns) {
$new[$table] = $this->intColumnHandler
->create($table, $columns, $index_columns[$table]);
}
foreach (array_filter($new) as $table => $columns) {
$this->connection
->update($table)
->fields([
reset($columns) => 0,
])
->execute();
}
}
}
}