5 * Installation functions for Content Translation module.
8 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
9 use Drupal\Core\Language\LanguageInterface;
13 * Implements hook_install().
15 function content_translation_install() {
16 // Assign a fairly low weight to ensure our implementation of
17 // hook_module_implements_alter() is run among the last ones.
18 module_set_weight('content_translation', 10);
20 // Translation works when at least two languages are added.
21 if (count(\Drupal::languageManager()->getLanguages()) < 2) {
23 ':language_url' => Url::fromRoute('entity.configurable_language.collection')->toString(),
25 $message = t('This site has only a single language enabled. <a href=":language_url">Add at least one more language</a> in order to translate content.', $t_args);
26 \Drupal::messenger()->addWarning($message);
28 // Point the user to the content translation settings.
30 ':settings_url' => Url::fromRoute('language.content_settings_page')->toString(),
32 $message = t('<a href=":settings_url">Enable translation</a> for <em>content types</em>, <em>taxonomy vocabularies</em>, <em>accounts</em>, or any other element you wish to translate.', $t_args);
33 \Drupal::messenger()->addWarning($message);
37 * Rebuild the routes as the content translation routes have now new names.
39 function content_translation_update_8001() {
40 \Drupal::service('router.builder')->rebuild();
44 * Clear field type plugin caches to fix image field translatability.
46 function content_translation_update_8002() {
47 \Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();
51 * Fix the initial values for content translation metadata fields.
53 function content_translation_update_8400() {
54 $database = \Drupal::database();
55 /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
56 $content_translation_manager = \Drupal::service('content_translation.manager');
57 /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
58 $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
59 $entity_type_manager = \Drupal::entityTypeManager();
60 $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
62 $entity_type_manager->clearCachedDefinitions();
63 foreach ($content_translation_manager->getSupportedEntityTypes() as $entity_type_id => $entity_type_definition) {
64 $storage = $entity_type_manager->getStorage($entity_type_id);
65 if ($storage instanceof SqlEntityStorageInterface) {
66 $entity_type = $entity_definition_update_manager->getEntityType($entity_type_id);
67 $storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
69 // Since the entity type is managed by Content Translation, we can assume
70 // that it is translatable, so we use the data and revision data tables.
71 $tables_to_update = [$entity_type->getDataTable()];
72 if ($entity_type->isRevisionable()) {
73 $tables_to_update += [$entity_type->getRevisionDataTable()];
76 foreach ($tables_to_update as $table_name) {
77 // Fix the values of the 'content_translation_source' field.
78 if (isset($storage_definitions['content_translation_source'])) {
79 $database->update($table_name)
80 ->fields(['content_translation_source' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
81 ->isNull('content_translation_source')
85 // Fix the values of the 'content_translation_outdated' field.
86 if (isset($storage_definitions['content_translation_outdated'])) {
87 $database->update($table_name)
88 ->fields(['content_translation_outdated' => 0])
89 ->isNull('content_translation_outdated')
93 // Fix the values of the 'content_translation_status' field.
94 if (isset($storage_definitions['content_translation_status'])) {
95 $database->update($table_name)
96 ->fields(['content_translation_status' => 1])
97 ->isNull('content_translation_status')