comparison core/modules/views/src/EventSubscriber/ViewsEntitySchemaSubscriber.php @ 18:af1871eacc83

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:33:08 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
17:129ea1e6d783 18:af1871eacc83
1 <?php 1 <?php
2 2
3 namespace Drupal\views\EventSubscriber; 3 namespace Drupal\views\EventSubscriber;
4 4
5 use Drupal\Core\Entity\EntityManagerInterface; 5 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
6 use Drupal\Core\Entity\EntityTypeEventSubscriberTrait; 6 use Drupal\Core\Entity\EntityTypeEventSubscriberTrait;
7 use Drupal\Core\Entity\EntityTypeInterface; 7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeListenerInterface; 8 use Drupal\Core\Entity\EntityTypeListenerInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Entity\Sql\SqlContentEntityStorage; 10 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
10 use Drupal\views\Views; 11 use Drupal\views\Views;
11 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
12 13
13 /** 14 /**
14 * Reacts to changes on entity types to update all views entities. 15 * Reacts to changes on entity types to update all views entities.
15 */ 16 */
16 class ViewsEntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface { 17 class ViewsEntitySchemaSubscriber implements EntityTypeListenerInterface, EventSubscriberInterface {
17 18
18 use EntityTypeEventSubscriberTrait; 19 use EntityTypeEventSubscriberTrait;
20 use DeprecatedServicePropertyTrait;
21
22 /**
23 * {@inheritdoc}
24 */
25 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
19 26
20 /** 27 /**
21 * Indicates that a base table got renamed. 28 * Indicates that a base table got renamed.
22 */ 29 */
23 const BASE_TABLE_RENAME = 0; 30 const BASE_TABLE_RENAME = 0;
66 * Indicates that a revision data table got removed. 73 * Indicates that a revision data table got removed.
67 */ 74 */
68 const REVISION_DATA_TABLE_REMOVAL = 9; 75 const REVISION_DATA_TABLE_REMOVAL = 9;
69 76
70 /** 77 /**
71 * The entity manager. 78 * The entity type manager.
72 * 79 *
73 * @var \Drupal\Core\Entity\EntityManagerInterface 80 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
74 */ 81 */
75 protected $entityManager; 82 protected $entityTypeManager;
76 83
77 /** 84 /**
78 * Constructs a ViewsEntitySchemaSubscriber. 85 * Constructs a ViewsEntitySchemaSubscriber.
79 * 86 *
80 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager 87 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
81 * The entity manager. 88 * The entity type manager.
82 */ 89 */
83 public function __construct(EntityManagerInterface $entity_manager) { 90 public function __construct(EntityTypeManagerInterface $entity_type_manager) {
84 $this->entityManager = $entity_manager; 91 $this->entityTypeManager = $entity_type_manager;
85 } 92 }
86 93
87 /** 94 /**
88 * {@inheritdoc} 95 * {@inheritdoc}
89 */ 96 */
97 public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) { 104 public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
98 $changes = []; 105 $changes = [];
99 106
100 // We implement a specific logic for table updates, which is bound to the 107 // We implement a specific logic for table updates, which is bound to the
101 // default sql content entity storage. 108 // default sql content entity storage.
102 if (!$this->entityManager->getStorage($entity_type->id()) instanceof SqlContentEntityStorage) { 109 if (!$this->entityTypeManager->getStorage($entity_type->id()) instanceof SqlContentEntityStorage) {
103 return; 110 return;
104 } 111 }
105 112
106 if ($entity_type->getBaseTable() != $original->getBaseTable()) { 113 if ($entity_type->getBaseTable() != $original->getBaseTable()) {
107 $changes[] = static::BASE_TABLE_RENAME; 114 $changes[] = static::BASE_TABLE_RENAME;
148 if (empty($changes)) { 155 if (empty($changes)) {
149 return; 156 return;
150 } 157 }
151 158
152 /** @var \Drupal\views\Entity\View[] $all_views */ 159 /** @var \Drupal\views\Entity\View[] $all_views */
153 $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL); 160 $all_views = $this->entityTypeManager->getStorage('view')->loadMultiple(NULL);
154 161
155 foreach ($changes as $change) { 162 foreach ($changes as $change) {
156 switch ($change) { 163 switch ($change) {
157 case static::BASE_TABLE_RENAME: 164 case static::BASE_TABLE_RENAME:
158 $this->baseTableRename($all_views, $entity_type->id(), $original->getBaseTable(), $entity_type->getBaseTable()); 165 $this->baseTableRename($all_views, $entity_type->id(), $original->getBaseTable(), $entity_type->getBaseTable());
205 $entity_type->getDataTable(), 212 $entity_type->getDataTable(),
206 $entity_type->getRevisionTable(), 213 $entity_type->getRevisionTable(),
207 $entity_type->getRevisionDataTable(), 214 $entity_type->getRevisionDataTable(),
208 ]; 215 ];
209 216
210 $all_views = $this->entityManager->getStorage('view')->loadMultiple(NULL); 217 $all_views = $this->entityTypeManager->getStorage('view')->loadMultiple(NULL);
211 /** @var \Drupal\views\Entity\View $view */ 218 /** @var \Drupal\views\Entity\View $view */
212 foreach ($all_views as $id => $view) { 219 foreach ($all_views as $id => $view) {
213 220
214 // First check just the base table. 221 // First check just the base table.
215 if (in_array($view->get('base_table'), $tables)) { 222 if (in_array($view->get('base_table'), $tables)) {
312 * The base table. 319 * The base table.
313 */ 320 */
314 protected function dataTableAddition($all_views, EntityTypeInterface $entity_type, $new_data_table, $base_table) { 321 protected function dataTableAddition($all_views, EntityTypeInterface $entity_type, $new_data_table, $base_table) {
315 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */ 322 /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $storage */
316 $entity_type_id = $entity_type->id(); 323 $entity_type_id = $entity_type->id();
317 $storage = $this->entityManager->getStorage($entity_type_id); 324 $storage = $this->entityTypeManager->getStorage($entity_type_id);
318 $storage->setEntityType($entity_type); 325 $storage->setEntityType($entity_type);
319 $table_mapping = $storage->getTableMapping(); 326 $table_mapping = $storage->getTableMapping();
320 $data_table_fields = $table_mapping->getFieldNames($new_data_table); 327 $data_table_fields = $table_mapping->getFieldNames($new_data_table);
321 $base_table_fields = $table_mapping->getFieldNames($base_table); 328 $base_table_fields = $table_mapping->getFieldNames($base_table);
322 329