summaryrefslogtreecommitdiffstats
path: root/core/modules/content_translation/content_translation.install
blob: 0e47270092177f48ca36b5d15e0084c064c9fcce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

/**
 * @file
 * Installation functions for Content Translation module.
 */

use Drupal\Core\Entity\Sql\SqlEntityStorageInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;

/**
 * Implements hook_install().
 */
function content_translation_install() {
  // Assign a fairly low weight to ensure our implementation of
  // hook_module_implements_alter() is run among the last ones.
  module_set_weight('content_translation', 10);

  // Translation works when at least two languages are added.
  if (count(\Drupal::languageManager()->getLanguages()) < 2) {
    $t_args = [
      ':language_url' => Url::fromRoute('entity.configurable_language.collection')->toString()
    ];
    $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);
    drupal_set_message($message, 'warning');
  }
  // Point the user to the content translation settings.
  $t_args = [
    ':settings_url' => Url::fromRoute('language.content_settings_page')->toString()
  ];
  $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);
  drupal_set_message($message, 'warning');
}

/**
 * Rebuild the routes as the content translation routes have now new names.
 */
function content_translation_update_8001() {
  \Drupal::service('router.builder')->rebuild();
}

/**
 * Clear field type plugin caches to fix image field translatability.
 */
function content_translation_update_8002() {
  \Drupal::service('plugin.manager.field.field_type')->clearCachedDefinitions();
}

/**
 * Fix the initial values for content translation metadata fields.
 */
function content_translation_update_8400() {
  $database = \Drupal::database();
  /** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
  $content_translation_manager = \Drupal::service('content_translation.manager');
  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
  $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();

  $entity_type_manager->clearCachedDefinitions();
  foreach ($content_translation_manager->getSupportedEntityTypes() as $entity_type_id => $entity_type_definition) {
    $storage = $entity_type_manager->getStorage($entity_type_id);
    if ($storage instanceof SqlEntityStorageInterface) {
      $entity_type = $entity_definition_update_manager->getEntityType($entity_type_id);
      $storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);

      // Since the entity type is managed by Content Translation, we can assume
      // that it is translatable, so we use the data and revision data tables.
      $tables_to_update = [$entity_type->getDataTable()];
      if ($entity_type->isRevisionable()) {
        $tables_to_update += [$entity_type->getRevisionDataTable()];
      }

      foreach ($tables_to_update as $table_name) {
        // Fix the values of the 'content_translation_source' field.
        if (isset($storage_definitions['content_translation_source'])) {
          $database->update($table_name)
            ->fields(['content_translation_source' => LanguageInterface::LANGCODE_NOT_SPECIFIED])
            ->isNull('content_translation_source')
            ->execute();
        }

        // Fix the values of the 'content_translation_outdated' field.
        if (isset($storage_definitions['content_translation_outdated'])) {
          $database->update($table_name)
            ->fields(['content_translation_outdated' => 0])
            ->isNull('content_translation_outdated')
            ->execute();
        }

        // Fix the values of the 'content_translation_status' field.
        if (isset($storage_definitions['content_translation_status'])) {
          $database->update($table_name)
            ->fields(['content_translation_status' => 1])
            ->isNull('content_translation_status')
            ->execute();
        }
      }
    }
  }
}