3 namespace Drupal\content_translation;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Config\ConfigEvents;
7 use Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\migrate\Event\MigrateEvents;
11 use Drupal\migrate\Event\MigrateImportEvent;
12 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
15 * Provides the logic needed to update field storage definitions when needed.
17 class ContentTranslationUpdatesManager implements EventSubscriberInterface {
22 * @var \Drupal\Core\Entity\EntityManagerInterface
24 protected $entityManager;
27 * The entity definition update manager.
29 * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
31 protected $updateManager;
34 * Constructs an updates manager instance.
36 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
38 * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $update_manager
39 * The entity definition update manager.
41 public function __construct(EntityManagerInterface $entity_manager, EntityDefinitionUpdateManagerInterface $update_manager) {
42 $this->entityManager = $entity_manager;
43 $this->updateManager = $update_manager;
47 * Executes field storage definition updates if needed.
49 * @param array $entity_types
50 * A list of entity type definitions to be processed.
52 public function updateDefinitions(array $entity_types) {
53 // Handle field storage definition creation, if needed.
54 // @todo Generalize this code in https://www.drupal.org/node/2346013.
55 // @todo Handle initial values in https://www.drupal.org/node/2346019.
56 if ($this->updateManager->needsUpdates()) {
57 foreach ($entity_types as $entity_type_id => $entity_type) {
58 $storage_definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
59 $installed_storage_definitions = $this->entityManager->getLastInstalledFieldStorageDefinitions($entity_type_id);
60 foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) {
61 /** @var $storage_definition \Drupal\Core\Field\FieldStorageDefinitionInterface */
62 if ($storage_definition->getProvider() == 'content_translation') {
63 $this->updateManager->installFieldStorageDefinition($storage_definition->getName(), $entity_type_id, 'content_translation', $storage_definition);
71 * Listener for the ConfigImporter import event.
73 public function onConfigImporterImport() {
74 $entity_types = array_filter($this->entityManager->getDefinitions(), function (EntityTypeInterface $entity_type) {
75 return $entity_type->isTranslatable();
77 $this->updateDefinitions($entity_types);
81 * Listener for migration imports.
83 public function onMigrateImport(MigrateImportEvent $event) {
84 $migration = $event->getMigration();
85 $configuration = $migration->getDestinationConfiguration();
86 $entity_types = NestedArray::getValue($configuration, ['content_translation_update_definitions']);
88 $entity_types = array_intersect_key($this->entityManager->getDefinitions(), array_flip($entity_types));
89 $this->updateDefinitions($entity_types);
96 public static function getSubscribedEvents() {
97 $events[ConfigEvents::IMPORT][] = ['onConfigImporterImport', 60];
98 if (class_exists('\Drupal\migrate\Event\MigrateEvents')) {
99 $events[MigrateEvents::POST_IMPORT][] = ['onMigrateImport'];