Security update for Core, with self-updated composer
[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_set_message($message, 'warning');
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_set_message($message, 'warning');
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   foreach ($content_translation_manager->getSupportedEntityTypes() as $entity_type_id => $entity_type_definition) {
63     $storage = $entity_type_manager->getStorage($entity_type_id);
64     if ($storage instanceof SqlEntityStorageInterface) {
65       $entity_type = $entity_definition_update_manager->getEntityType($entity_type_id);
66       $storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
67
68       // Since the entity type is managed by Content Translation, we can assume
69       // that it is translatable, so we use the data and revision data tables.
70       $tables_to_update = [$entity_type->getDataTable()];
71       if ($entity_type->isRevisionable()) {
72         $tables_to_update += [$entity_type->getRevisionDataTable()];
73       }
74
75       foreach ($tables_to_update as $table_name) {
76         // Fix the values of the 'content_translation_source' field.
77         if (isset($storage_definitions['content_translation_source'])) {
78           $database->update($table_name)
79             ->fields(['content_translation_source' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
80             ->isNull('content_translation_source')
81             ->execute();
82         }
83
84         // Fix the values of the 'content_translation_outdated' field.
85         if (isset($storage_definitions['content_translation_outdated'])) {
86           $database->update($table_name)
87             ->fields(['content_translation_outdated' => 0])
88             ->isNull('content_translation_outdated')
89             ->execute();
90         }
91
92         // Fix the values of the 'content_translation_status' field.
93         if (isset($storage_definitions['content_translation_status'])) {
94           $database->update($table_name)
95             ->fields(['content_translation_status' => 1])
96             ->isNull('content_translation_status')
97             ->execute();
98         }
99       }
100     }
101   }
102 }