Backup of db before drupal security update
[yaffs-website] / web / core / modules / content_translation / src / ContentTranslationUpdatesManager.php
1 <?php
2
3 namespace Drupal\content_translation;
4
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;
13
14 /**
15  * Provides the logic needed to update field storage definitions when needed.
16  */
17 class ContentTranslationUpdatesManager implements EventSubscriberInterface {
18
19   /**
20    * The entity manager.
21    *
22    * @var \Drupal\Core\Entity\EntityManagerInterface
23    */
24   protected $entityManager;
25
26   /**
27    * The entity definition update manager.
28    *
29    * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface
30    */
31   protected $updateManager;
32
33   /**
34    * Constructs an updates manager instance.
35    *
36    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
37    *   The entity manager.
38    * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $update_manager
39    *   The entity definition update manager.
40    */
41   public function __construct(EntityManagerInterface $entity_manager, EntityDefinitionUpdateManagerInterface $update_manager) {
42     $this->entityManager = $entity_manager;
43     $this->updateManager = $update_manager;
44   }
45
46   /**
47    * Executes field storage definition updates if needed.
48    *
49    * @param array $entity_types
50    *   A list of entity type definitions to be processed.
51    */
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);
64           }
65         }
66       }
67     }
68   }
69
70   /**
71    * Listener for the ConfigImporter import event.
72    */
73   public function onConfigImporterImport() {
74     $entity_types = array_filter($this->entityManager->getDefinitions(), function (EntityTypeInterface $entity_type) {
75       return $entity_type->isTranslatable();
76     });
77     $this->updateDefinitions($entity_types);
78   }
79
80   /**
81    * Listener for migration imports.
82    */
83   public function onMigrateImport(MigrateImportEvent $event) {
84     $migration = $event->getMigration();
85     $configuration = $migration->getDestinationConfiguration();
86     $entity_types = NestedArray::getValue($configuration, ['content_translation_update_definitions']);
87     if ($entity_types) {
88       $entity_types = array_intersect_key($this->entityManager->getDefinitions(), array_flip($entity_types));
89       $this->updateDefinitions($entity_types);
90     }
91   }
92
93   /**
94    * {@inheritdoc}
95    */
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'];
100     }
101     return $events;
102   }
103
104 }