EntityDefinitionUpdateManager.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. <?php
  2. namespace Drupal\Core\Entity;
  3. use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
  4. use Drupal\Core\Entity\Schema\DynamicallyFieldableEntityStorageSchemaInterface;
  5. use Drupal\Core\Entity\Schema\EntityStorageSchemaInterface;
  6. use Drupal\Core\Field\BaseFieldDefinition;
  7. use Drupal\Core\Field\FieldStorageDefinitionInterface;
  8. use Drupal\Core\Field\FieldStorageDefinitionListenerInterface;
  9. use Drupal\Core\StringTranslation\StringTranslationTrait;
  10. /**
  11. * Manages entity definition updates.
  12. */
  13. class EntityDefinitionUpdateManager implements EntityDefinitionUpdateManagerInterface {
  14. use StringTranslationTrait;
  15. use DeprecatedServicePropertyTrait;
  16. /**
  17. * {@inheritdoc}
  18. */
  19. protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
  20. /**
  21. * The entity field manager service.
  22. *
  23. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  24. */
  25. protected $entityFieldManager;
  26. /**
  27. * The entity type listener service.
  28. *
  29. * @var \Drupal\Core\Entity\EntityTypeListenerInterface
  30. */
  31. protected $entityTypeListener;
  32. /**
  33. * The entity type manager service.
  34. *
  35. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  36. */
  37. protected $entityTypeManager;
  38. /**
  39. * The field storage definition listener service.
  40. *
  41. * @var \Drupal\Core\Field\FieldStorageDefinitionListenerInterface
  42. */
  43. protected $fieldStorageDefinitionListener;
  44. /**
  45. * The last installed schema repository.
  46. *
  47. * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
  48. */
  49. protected $entityLastInstalledSchemaRepository;
  50. /**
  51. * Constructs a new EntityDefinitionUpdateManager.
  52. *
  53. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  54. * The entity type manager service.
  55. * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
  56. * The last installed schema repository service.
  57. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
  58. * The entity field manager service.
  59. * @param \Drupal\Core\Entity\EntityTypeListenerInterface $entity_type_listener
  60. * The entity type listener interface.
  61. * @param \Drupal\Core\Field\FieldStorageDefinitionListenerInterface $field_storage_definition_listener
  62. * The field storage definition listener service.
  63. */
  64. public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository = NULL, EntityFieldManagerInterface $entity_field_manager = NULL, EntityTypeListenerInterface $entity_type_listener = NULL, FieldStorageDefinitionListenerInterface $field_storage_definition_listener = NULL) {
  65. if ($entity_type_manager instanceof EntityManagerInterface) {
  66. @trigger_error('Passing the entity.manager service to EntityDefinitionUpdateManager::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  67. $this->entityTypeManager = \Drupal::entityTypeManager();
  68. }
  69. else {
  70. $this->entityTypeManager = $entity_type_manager;
  71. }
  72. if ($entity_last_installed_schema_repository) {
  73. $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
  74. }
  75. else {
  76. @trigger_error('The entity.last_installed_schema.repository service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  77. $this->entityLastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository');
  78. }
  79. if ($entity_field_manager) {
  80. $this->entityFieldManager = $entity_field_manager;
  81. }
  82. else {
  83. @trigger_error('The entity_field.manager service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  84. $this->entityFieldManager = \Drupal::service('entity_field.manager');
  85. }
  86. if ($entity_type_listener) {
  87. $this->entityTypeListener = $entity_type_listener;
  88. }
  89. else {
  90. @trigger_error('The entity_type.listener service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  91. $this->entityTypeListener = \Drupal::service('entity_type.listener');
  92. }
  93. if ($field_storage_definition_listener) {
  94. $this->fieldStorageDefinitionListener = $field_storage_definition_listener;
  95. }
  96. else {
  97. @trigger_error('The field_storage_definition.listener service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
  98. $this->fieldStorageDefinitionListener = \Drupal::service('field_storage_definition.listener');
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function needsUpdates() {
  105. return (bool) $this->getChangeList();
  106. }
  107. /**
  108. * {@inheritdoc}
  109. */
  110. public function getChangeSummary() {
  111. $summary = [];
  112. foreach ($this->getChangeList() as $entity_type_id => $change_list) {
  113. // Process entity type definition changes.
  114. if (!empty($change_list['entity_type'])) {
  115. $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
  116. switch ($change_list['entity_type']) {
  117. case static::DEFINITION_CREATED:
  118. $summary[$entity_type_id][] = $this->t('The %entity_type entity type needs to be installed.', ['%entity_type' => $entity_type->getLabel()]);
  119. break;
  120. case static::DEFINITION_UPDATED:
  121. $summary[$entity_type_id][] = $this->t('The %entity_type entity type needs to be updated.', ['%entity_type' => $entity_type->getLabel()]);
  122. break;
  123. }
  124. }
  125. // Process field storage definition changes.
  126. if (!empty($change_list['field_storage_definitions'])) {
  127. $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id);
  128. $original_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id);
  129. foreach ($change_list['field_storage_definitions'] as $field_name => $change) {
  130. switch ($change) {
  131. case static::DEFINITION_CREATED:
  132. $summary[$entity_type_id][] = $this->t('The %field_name field needs to be installed.', ['%field_name' => $storage_definitions[$field_name]->getLabel() ?: $field_name]);
  133. break;
  134. case static::DEFINITION_UPDATED:
  135. $summary[$entity_type_id][] = $this->t('The %field_name field needs to be updated.', ['%field_name' => $storage_definitions[$field_name]->getLabel() ?: $field_name]);
  136. break;
  137. case static::DEFINITION_DELETED:
  138. $summary[$entity_type_id][] = $this->t('The %field_name field needs to be uninstalled.', ['%field_name' => $original_storage_definitions[$field_name]->getLabel() ?: $field_name]);
  139. break;
  140. }
  141. }
  142. }
  143. }
  144. return $summary;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function applyUpdates() {
  150. trigger_error('EntityDefinitionUpdateManagerInterface::applyUpdates() is deprecated in 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface::getChangeList() and execute each entity type and field storage update manually instead. See https://www.drupal.org/node/3034742.', E_USER_DEPRECATED);
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getEntityType($entity_type_id) {
  156. $entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id);
  157. return $entity_type ? clone $entity_type : NULL;
  158. }
  159. /**
  160. * {@inheritdoc}
  161. */
  162. public function getEntityTypes() {
  163. return $this->entityLastInstalledSchemaRepository->getLastInstalledDefinitions();
  164. }
  165. /**
  166. * {@inheritdoc}
  167. */
  168. public function installEntityType(EntityTypeInterface $entity_type) {
  169. $this->clearCachedDefinitions();
  170. $this->entityTypeListener->onEntityTypeCreate($entity_type);
  171. }
  172. /**
  173. * {@inheritdoc}
  174. */
  175. public function updateEntityType(EntityTypeInterface $entity_type) {
  176. $original = $this->getEntityType($entity_type->id());
  177. $this->clearCachedDefinitions();
  178. $this->entityTypeListener->onEntityTypeUpdate($entity_type, $original);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function uninstallEntityType(EntityTypeInterface $entity_type) {
  184. $this->clearCachedDefinitions();
  185. $this->entityTypeListener->onEntityTypeDelete($entity_type);
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function installFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions) {
  191. $this->clearCachedDefinitions();
  192. foreach ($field_storage_definitions as $name => $field_storage_definition) {
  193. if ($field_storage_definition instanceof BaseFieldDefinition) {
  194. $field_storage_definition
  195. ->setName($name)
  196. ->setTargetEntityTypeId($entity_type->id())
  197. ->setProvider($entity_type->getProvider())
  198. ->setTargetBundle(NULL);
  199. }
  200. }
  201. $this->entityTypeListener->onFieldableEntityTypeCreate($entity_type, $field_storage_definitions);
  202. }
  203. /**
  204. * {@inheritdoc}
  205. */
  206. public function updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array &$sandbox = NULL) {
  207. $original = $this->getEntityType($entity_type->id());
  208. if ($this->requiresEntityDataMigration($entity_type, $original) && $sandbox === NULL) {
  209. throw new \InvalidArgumentException('The entity schema update for the ' . $entity_type->id() . ' entity type requires a data migration.');
  210. }
  211. $original_field_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type->id());
  212. $this->entityTypeListener->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox);
  213. $this->clearCachedDefinitions();
  214. }
  215. /**
  216. * {@inheritdoc}
  217. */
  218. public function installFieldStorageDefinition($name, $entity_type_id, $provider, FieldStorageDefinitionInterface $storage_definition) {
  219. // @todo Pass a mutable field definition interface when we have one. See
  220. // https://www.drupal.org/node/2346329.
  221. if ($storage_definition instanceof BaseFieldDefinition) {
  222. $storage_definition
  223. ->setName($name)
  224. ->setTargetEntityTypeId($entity_type_id)
  225. ->setProvider($provider)
  226. ->setTargetBundle(NULL);
  227. }
  228. $this->clearCachedDefinitions();
  229. $this->fieldStorageDefinitionListener->onFieldStorageDefinitionCreate($storage_definition);
  230. }
  231. /**
  232. * {@inheritdoc}
  233. */
  234. public function getFieldStorageDefinition($name, $entity_type_id) {
  235. $storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id);
  236. return isset($storage_definitions[$name]) ? clone $storage_definitions[$name] : NULL;
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
  242. $original = $this->getFieldStorageDefinition($storage_definition->getName(), $storage_definition->getTargetEntityTypeId());
  243. $this->clearCachedDefinitions();
  244. $this->fieldStorageDefinitionListener->onFieldStorageDefinitionUpdate($storage_definition, $original);
  245. }
  246. /**
  247. * {@inheritdoc}
  248. */
  249. public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) {
  250. $this->clearCachedDefinitions();
  251. $this->fieldStorageDefinitionListener->onFieldStorageDefinitionDelete($storage_definition);
  252. }
  253. /**
  254. * {@inheritdoc}
  255. */
  256. public function getChangeList() {
  257. $this->entityTypeManager->useCaches(FALSE);
  258. $this->entityFieldManager->useCaches(FALSE);
  259. $change_list = [];
  260. foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
  261. $original = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id);
  262. // @todo Support non-storage-schema-changing definition updates too:
  263. // https://www.drupal.org/node/2336895.
  264. if (!$original) {
  265. $change_list[$entity_type_id]['entity_type'] = static::DEFINITION_CREATED;
  266. }
  267. else {
  268. if ($this->requiresEntityStorageSchemaChanges($entity_type, $original)) {
  269. $change_list[$entity_type_id]['entity_type'] = static::DEFINITION_UPDATED;
  270. }
  271. if ($this->entityTypeManager->getStorage($entity_type_id) instanceof DynamicallyFieldableEntityStorageInterface) {
  272. $field_changes = [];
  273. $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id);
  274. $original_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id);
  275. // Detect created field storage definitions.
  276. foreach (array_diff_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) {
  277. $field_changes[$field_name] = static::DEFINITION_CREATED;
  278. }
  279. // Detect deleted field storage definitions.
  280. foreach (array_diff_key($original_storage_definitions, $storage_definitions) as $field_name => $original_storage_definition) {
  281. $field_changes[$field_name] = static::DEFINITION_DELETED;
  282. }
  283. // Detect updated field storage definitions.
  284. foreach (array_intersect_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) {
  285. // @todo Support non-storage-schema-changing definition updates too:
  286. // https://www.drupal.org/node/2336895. So long as we're checking
  287. // based on schema change requirements rather than definition
  288. // equality, skip the check if the entity type itself needs to be
  289. // updated, since that can affect the schema of all fields, so we
  290. // want to process that update first without reporting false
  291. // positives here.
  292. if (!isset($change_list[$entity_type_id]['entity_type']) && $this->requiresFieldStorageSchemaChanges($storage_definition, $original_storage_definitions[$field_name])) {
  293. $field_changes[$field_name] = static::DEFINITION_UPDATED;
  294. }
  295. }
  296. if ($field_changes) {
  297. $change_list[$entity_type_id]['field_storage_definitions'] = $field_changes;
  298. }
  299. }
  300. }
  301. }
  302. // @todo Support deleting entity definitions when we support base field
  303. // purging.
  304. // @see https://www.drupal.org/node/2907779
  305. $this->entityTypeManager->useCaches(TRUE);
  306. $this->entityFieldManager->useCaches(TRUE);
  307. return array_filter($change_list);
  308. }
  309. /**
  310. * Checks if the changes to the entity type requires storage schema changes.
  311. *
  312. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  313. * The updated entity type definition.
  314. * @param \Drupal\Core\Entity\EntityTypeInterface $original
  315. * The original entity type definition.
  316. *
  317. * @return bool
  318. * TRUE if storage schema changes are required, FALSE otherwise.
  319. */
  320. protected function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  321. $storage = $this->entityTypeManager->getStorage($entity_type->id());
  322. return ($storage instanceof EntityStorageSchemaInterface) && $storage->requiresEntityStorageSchemaChanges($entity_type, $original);
  323. }
  324. /**
  325. * Checks if the changes to the storage definition requires schema changes.
  326. *
  327. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition
  328. * The updated field storage definition.
  329. * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original
  330. * The original field storage definition.
  331. *
  332. * @return bool
  333. * TRUE if storage schema changes are required, FALSE otherwise.
  334. */
  335. protected function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
  336. $storage = $this->entityTypeManager->getStorage($storage_definition->getTargetEntityTypeId());
  337. return ($storage instanceof DynamicallyFieldableEntityStorageSchemaInterface) && $storage->requiresFieldStorageSchemaChanges($storage_definition, $original);
  338. }
  339. /**
  340. * Checks if existing data would be lost if the schema changes were applied.
  341. *
  342. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  343. * The updated entity type definition.
  344. * @param \Drupal\Core\Entity\EntityTypeInterface $original
  345. * The original entity type definition.
  346. *
  347. * @return bool
  348. * TRUE if data migration is required, FALSE otherwise.
  349. */
  350. protected function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) {
  351. $storage = $this->entityTypeManager->getStorage($entity_type->id());
  352. return ($storage instanceof EntityStorageSchemaInterface) && $storage->requiresEntityDataMigration($entity_type, $original);
  353. }
  354. /**
  355. * Clears necessary caches to apply entity/field definition updates.
  356. */
  357. protected function clearCachedDefinitions() {
  358. $this->entityTypeManager->clearCachedDefinitions();
  359. $this->entityFieldManager->clearCachedFieldDefinitions();
  360. }
  361. }