Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /**
|
Chris@0
|
4 * @file
|
Chris@0
|
5 * Installation functions for Content Translation module.
|
Chris@0
|
6 */
|
Chris@0
|
7
|
Chris@0
|
8 use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
|
Chris@0
|
9 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
10 use Drupal\Core\Url;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Implements hook_install().
|
Chris@0
|
14 */
|
Chris@0
|
15 function content_translation_install() {
|
Chris@0
|
16 // Assign a fairly low weight to ensure our implementation of
|
Chris@0
|
17 // hook_module_implements_alter() is run among the last ones.
|
Chris@0
|
18 module_set_weight('content_translation', 10);
|
Chris@0
|
19
|
Chris@18
|
20 // Skip the guidance messages about enabling translation features if the
|
Chris@18
|
21 // module was installed in the Drupal installation process.
|
Chris@18
|
22 if (drupal_installation_attempted()) {
|
Chris@18
|
23 return;
|
Chris@18
|
24 }
|
Chris@18
|
25
|
Chris@0
|
26 // Translation works when at least two languages are added.
|
Chris@0
|
27 if (count(\Drupal::languageManager()->getLanguages()) < 2) {
|
Chris@0
|
28 $t_args = [
|
Chris@17
|
29 ':language_url' => Url::fromRoute('entity.configurable_language.collection')->toString(),
|
Chris@0
|
30 ];
|
Chris@0
|
31 $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);
|
Chris@17
|
32 \Drupal::messenger()->addWarning($message);
|
Chris@0
|
33 }
|
Chris@0
|
34 // Point the user to the content translation settings.
|
Chris@0
|
35 $t_args = [
|
Chris@17
|
36 ':settings_url' => Url::fromRoute('language.content_settings_page')->toString(),
|
Chris@0
|
37 ];
|
Chris@0
|
38 $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);
|
Chris@17
|
39 \Drupal::messenger()->addWarning($message);
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Rebuild the routes as the content translation routes have now new names.
|
Chris@0
|
44 */
|
Chris@0
|
45 function content_translation_update_8001() {
|
Chris@0
|
46 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Clear field type plugin caches to fix image field translatability.
|
Chris@0
|
51 */
|
Chris@0
|
52 function content_translation_update_8002() {
|
Chris@0
|
53 \Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();
|
Chris@0
|
54 }
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Fix the initial values for content translation metadata fields.
|
Chris@0
|
58 */
|
Chris@0
|
59 function content_translation_update_8400() {
|
Chris@0
|
60 $database = \Drupal::database();
|
Chris@0
|
61 /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
|
Chris@0
|
62 $content_translation_manager = \Drupal::service('content_translation.manager');
|
Chris@0
|
63 /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
|
Chris@0
|
64 $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
|
Chris@0
|
65 $entity_type_manager = \Drupal::entityTypeManager();
|
Chris@0
|
66 $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
|
Chris@0
|
67
|
Chris@14
|
68 $entity_type_manager->clearCachedDefinitions();
|
Chris@0
|
69 foreach ($content_translation_manager->getSupportedEntityTypes() as $entity_type_id => $entity_type_definition) {
|
Chris@0
|
70 $storage = $entity_type_manager->getStorage($entity_type_id);
|
Chris@0
|
71 if ($storage instanceof SqlEntityStorageInterface) {
|
Chris@0
|
72 $entity_type = $entity_definition_update_manager->getEntityType($entity_type_id);
|
Chris@0
|
73 $storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
|
Chris@0
|
74
|
Chris@0
|
75 // Since the entity type is managed by Content Translation, we can assume
|
Chris@0
|
76 // that it is translatable, so we use the data and revision data tables.
|
Chris@0
|
77 $tables_to_update = [$entity_type->getDataTable()];
|
Chris@0
|
78 if ($entity_type->isRevisionable()) {
|
Chris@0
|
79 $tables_to_update += [$entity_type->getRevisionDataTable()];
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 foreach ($tables_to_update as $table_name) {
|
Chris@0
|
83 // Fix the values of the 'content_translation_source' field.
|
Chris@0
|
84 if (isset($storage_definitions['content_translation_source'])) {
|
Chris@0
|
85 $database->update($table_name)
|
Chris@0
|
86 ->fields(['content_translation_source' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
|
Chris@0
|
87 ->isNull('content_translation_source')
|
Chris@0
|
88 ->execute();
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 // Fix the values of the 'content_translation_outdated' field.
|
Chris@0
|
92 if (isset($storage_definitions['content_translation_outdated'])) {
|
Chris@0
|
93 $database->update($table_name)
|
Chris@0
|
94 ->fields(['content_translation_outdated' => 0])
|
Chris@0
|
95 ->isNull('content_translation_outdated')
|
Chris@0
|
96 ->execute();
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 // Fix the values of the 'content_translation_status' field.
|
Chris@0
|
100 if (isset($storage_definitions['content_translation_status'])) {
|
Chris@0
|
101 $database->update($table_name)
|
Chris@0
|
102 ->fields(['content_translation_status' => 1])
|
Chris@0
|
103 ->isNull('content_translation_status')
|
Chris@0
|
104 ->execute();
|
Chris@0
|
105 }
|
Chris@0
|
106 }
|
Chris@0
|
107 }
|
Chris@0
|
108 }
|
Chris@0
|
109 }
|