ContentLanguageSettings.php in Acquia Content Hub 8.2
File
src/EventSubscriber/EntityImport/ContentLanguageSettings.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\EntityImport;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\EntityImportEvent;
use Drupal\language\ContentLanguageSettingsInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ContentLanguageSettings implements EventSubscriberInterface {
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::ENTITY_IMPORT_NEW][] = 'onImportNew';
$events[AcquiaContentHubEvents::ENTITY_IMPORT_UPDATE][] = 'onImportUpdate';
return $events;
}
public function onImportNew(EntityImportEvent $event) {
$settings = $event
->getEntity();
if (!$settings instanceof ContentLanguageSettingsInterface) {
return;
}
if ($settings
->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
$this
->processSettings($settings);
}
}
public function onImportUpdate(EntityImportEvent $event) {
$settings = $event
->getEntity();
if (!$settings instanceof ContentLanguageSettingsInterface) {
return;
}
$original_settings = $settings
->get('original');
if (!$original_settings) {
$this
->onImportNew($event);
return;
}
if ($settings
->getThirdPartySetting('content_translation', 'enabled', FALSE) && !$original_settings
->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
$this
->processSettings($settings);
}
}
protected function processSettings(ContentLanguageSettingsInterface $settings) {
$field_manager = \Drupal::service('entity_field.manager');
$schema_repository = \Drupal::service('entity.last_installed_schema.repository');
$definition_update_manager = \Drupal::entityDefinitionUpdateManager();
$entity_type_id = $settings
->getTargetEntityTypeId();
$field_manager
->useCaches(FALSE);
$storage_definitions = $field_manager
->getFieldStorageDefinitions($entity_type_id);
$field_manager
->useCaches(TRUE);
$installed_storage_definitions = $schema_repository
->getLastInstalledFieldStorageDefinitions($entity_type_id);
foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) {
if ($storage_definition
->getProvider() == 'content_translation') {
$definition_update_manager
->installFieldStorageDefinition($storage_definition
->getName(), $entity_type_id, 'content_translation', $storage_definition);
}
}
}
}