Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_translation / content_translation.install
1 <?php
2
3 /**
4  * @file
5  * Installation functions for Content Translation module.
6  */
7
8 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\Core\Url;
11
12 /**
13  * Implements hook_install().
14  */
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);
19
20   // Translation works when at least two languages are added.
21   if (count(\Drupal::languageManager()->getLanguages()) < 2) {
22     $t_args = [
23       ':language_url' => Url::fromRoute('entity.configurable_language.collection')->toString(),
24     ];
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);
27   }
28   // Point the user to the content translation settings.
29   $t_args = [
30     ':settings_url' => Url::fromRoute('language.content_settings_page')->toString(),
31   ];
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);
34 }
35
36 /**
37  * Rebuild the routes as the content translation routes have now new names.
38  */
39 function content_translation_update_8001() {
40   \Drupal::service('router.builder')->rebuild();
41 }
42
43 /**
44  * Clear field type plugin caches to fix image field translatability.
45  */
46 function content_translation_update_8002() {
47   \Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();
48 }
49
50 /**
51  * Fix the initial values for content translation metadata fields.
52  */
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();
61
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);
68
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()];
74       }
75
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')
82             ->execute();
83         }
84
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')
90             ->execute();
91         }
92
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')
98             ->execute();
99         }
100       }
101     }
102   }
103 }