content_translation.install 4.2 KB

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