View source
<?php
namespace Drupal\views\EventSubscriber;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeListenerInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\views\Views;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ViewsEntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
use EntityTypeEventSubscriberTrait;
const BASE_TABLE_RENAME = 0;
const DATA_TABLE_RENAME = 1;
const DATA_TABLE_ADDITION = 2;
const DATA_TABLE_REMOVAL = 3;
const REVISION_TABLE_RENAME = 4;
const REVISION_TABLE_ADDITION = 5;
const REVISION_TABLE_REMOVAL = 6;
const REVISION_DATA_TABLE_RENAME = 7;
const REVISION_DATA_TABLE_ADDITION = 8;
const REVISION_DATA_TABLE_REMOVAL = 9;
protected $entityManager;
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
public static function getSubscribedEvents() {
return static::getEntityTypeEvents();
}
public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
$changes = [];
if (!$this->entityManager
->getStorage($entity_type
->id()) instanceof SqlContentEntityStorage) {
return;
}
if ($entity_type
->getBaseTable() != $original
->getBaseTable()) {
$changes[] = static::BASE_TABLE_RENAME;
}
$revision_add = $entity_type
->isRevisionable() && !$original
->isRevisionable();
$revision_remove = !$entity_type
->isRevisionable() && $original
->isRevisionable();
$translation_add = $entity_type
->isTranslatable() && !$original
->isTranslatable();
$translation_remove = !$entity_type
->isTranslatable() && $original
->isTranslatable();
if ($revision_add) {
$changes[] = static::REVISION_TABLE_ADDITION;
}
elseif ($revision_remove) {
$changes[] = static::REVISION_TABLE_REMOVAL;
}
elseif ($entity_type
->isRevisionable() && $entity_type
->getRevisionTable() != $original
->getRevisionTable()) {
$changes[] = static::REVISION_TABLE_RENAME;
}
if ($translation_add) {
$changes[] = static::DATA_TABLE_ADDITION;
}
elseif ($translation_remove) {
$changes[] = static::DATA_TABLE_REMOVAL;
}
elseif ($entity_type
->isTranslatable() && $entity_type
->getDataTable() != $original
->getDataTable()) {
$changes[] = static::DATA_TABLE_RENAME;
}
if ($entity_type
->isRevisionable() && $entity_type
->isTranslatable()) {
if ($revision_add || $translation_add) {
$changes[] = static::REVISION_DATA_TABLE_ADDITION;
}
elseif ($entity_type
->getRevisionDataTable() != $original
->getRevisionDataTable()) {
$changes[] = static::REVISION_DATA_TABLE_RENAME;
}
}
elseif ($original
->isRevisionable() && $original
->isTranslatable() && ($revision_remove || $translation_remove)) {
$changes[] = static::REVISION_DATA_TABLE_REMOVAL;
}
$all_views = $this->entityManager
->getStorage('view')
->loadMultiple(NULL);
foreach ($changes as $change) {
switch ($change) {
case static::BASE_TABLE_RENAME:
$this
->baseTableRename($all_views, $entity_type
->id(), $original
->getBaseTable(), $entity_type
->getBaseTable());
break;
case static::DATA_TABLE_RENAME:
$this
->dataTableRename($all_views, $entity_type
->id(), $original
->getDataTable(), $entity_type
->getDataTable());
break;
case static::DATA_TABLE_ADDITION:
$this
->dataTableAddition($all_views, $entity_type, $entity_type
->getDataTable(), $entity_type
->getBaseTable());
break;
case static::DATA_TABLE_REMOVAL:
$this
->dataTableRemoval($all_views, $entity_type
->id(), $original
->getDataTable(), $entity_type
->getBaseTable());
break;
case static::REVISION_TABLE_RENAME:
$this
->baseTableRename($all_views, $entity_type
->id(), $original
->getRevisionTable(), $entity_type
->getRevisionTable());
break;
case static::REVISION_TABLE_ADDITION:
break;
case static::REVISION_TABLE_REMOVAL:
$this
->revisionRemoval($all_views, $original);
break;
case static::REVISION_DATA_TABLE_RENAME:
$this
->dataTableRename($all_views, $entity_type
->id(), $original
->getRevisionDataTable(), $entity_type
->getRevisionDataTable());
break;
case static::REVISION_DATA_TABLE_ADDITION:
$this
->dataTableAddition($all_views, $entity_type, $entity_type
->getRevisionDataTable(), $entity_type
->getRevisionTable());
break;
case static::REVISION_DATA_TABLE_REMOVAL:
$this
->dataTableRemoval($all_views, $entity_type
->id(), $original
->getRevisionDataTable(), $entity_type
->getRevisionTable());
break;
}
}
foreach ($all_views as $view) {
$view
->trustData()
->save();
}
}
public function onEntityTypeDelete(EntityTypeInterface $entity_type) {
$tables = [
$entity_type
->getBaseTable(),
$entity_type
->getDataTable(),
$entity_type
->getRevisionTable(),
$entity_type
->getRevisionDataTable(),
];
$all_views = $this->entityManager
->getStorage('view')
->loadMultiple(NULL);
foreach ($all_views as $id => $view) {
if (in_array($view
->get('base_table'), $tables)) {
$view
->disable();
$view
->save();
}
}
}
protected function processHandlers(array $all_views, callable $process) {
foreach ($all_views as $view) {
foreach (array_keys($view
->get('display')) as $display_id) {
$display =& $view
->getDisplay($display_id);
foreach (Views::getHandlerTypes() as $handler_type) {
$handler_type = $handler_type['plural'];
if (!isset($display['display_options'][$handler_type])) {
continue;
}
foreach ($display['display_options'][$handler_type] as $id => &$handler_config) {
$process($handler_config);
if ($handler_config === NULL) {
unset($display['display_options'][$handler_type][$id]);
}
}
}
}
}
}
protected function baseTableRename($all_views, $entity_type_id, $old_base_table, $new_base_table) {
foreach ($all_views as $view) {
if ($view
->get('base_table') == $old_base_table) {
$view
->set('base_table', $new_base_table);
}
}
$this
->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_base_table, $new_base_table) {
if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_base_table) {
$handler_config['table'] = $new_base_table;
}
});
}
protected function dataTableRename($all_views, $entity_type_id, $old_data_table, $new_data_table) {
foreach ($all_views as $view) {
if ($view
->get('base_table') == $old_data_table) {
$view
->set('base_table', $new_data_table);
}
}
$this
->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_data_table, $new_data_table) {
if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id && $handler_config['table'] == $old_data_table) {
$handler_config['table'] = $new_data_table;
}
});
}
protected function dataTableAddition($all_views, EntityTypeInterface $entity_type, $new_data_table, $base_table) {
$entity_type_id = $entity_type
->id();
$storage = $this->entityManager
->getStorage($entity_type_id);
$storage
->setEntityType($entity_type);
$table_mapping = $storage
->getTableMapping();
$data_table_fields = $table_mapping
->getFieldNames($new_data_table);
$base_table_fields = $table_mapping
->getFieldNames($base_table);
$data_table = $new_data_table;
$this
->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $base_table, $data_table, $base_table_fields, $data_table_fields) {
if (isset($handler_config['entity_type']) && isset($handler_config['entity_field']) && $handler_config['entity_type'] == $entity_type_id) {
if ($handler_config['table'] == $base_table && in_array($handler_config['entity_field'], $data_table_fields) && !in_array($handler_config['entity_field'], $base_table_fields)) {
$handler_config['table'] = $data_table;
}
}
});
}
protected function dataTableRemoval($all_views, $entity_type_id, $old_data_table, $base_table) {
$this
->processHandlers($all_views, function (array &$handler_config) use ($entity_type_id, $old_data_table, $base_table) {
if (isset($handler_config['entity_type']) && $handler_config['entity_type'] == $entity_type_id) {
if ($handler_config['table'] == $old_data_table) {
$handler_config['table'] = $base_table;
}
}
});
}
protected function revisionRemoval($all_views, EntityTypeInterface $original) {
$revision_base_table = $original
->getRevisionTable();
$revision_data_table = $original
->getRevisionDataTable();
foreach ($all_views as $view) {
if (in_array($view
->get('base_table'), [
$revision_base_table,
$revision_data_table,
])) {
$view
->setStatus(FALSE);
}
}
}
}