content_translation.install 4.5 KB

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