diff core/modules/content_translation/content_translation.module @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
line wrap: on
line diff
--- a/core/modules/content_translation/content_translation.module	Thu Feb 28 13:11:55 2019 +0000
+++ b/core/modules/content_translation/content_translation.module	Thu May 09 15:34:47 2019 +0100
@@ -5,6 +5,7 @@
  * Allows entities to be translated into different languages.
  */
 
+use Drupal\Core\Url;
 use Drupal\content_translation\BundleTranslationSettingsInterface;
 use Drupal\content_translation\ContentTranslationManager;
 use Drupal\Core\Access\AccessResult;
@@ -16,6 +17,7 @@
 use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\language\ContentLanguageSettingsInterface;
 
 /**
  * Implements hook_help().
@@ -25,11 +27,11 @@
     case 'help.page.content_translation':
       $output = '';
       $output .= '<h3>' . t('About') . '</h3>';
-      $output .= '<p>' . t('The Content Translation module allows you to translate content, comments, custom blocks, taxonomy terms, users and other <a href=":field_help" title="Field module help, with background on content entities">content entities</a>. Together with the modules <a href=":language">Language</a>, <a href=":config-trans">Configuration Translation</a>, and <a href=":locale">Interface Translation</a>, it allows you to build multilingual websites. For more information, see the <a href=":translation-entity">online documentation for the Content Translation module</a>.', [':locale' => (\Drupal::moduleHandler()->moduleExists('locale')) ? \Drupal::url('help.page', ['name' => 'locale']) : '#', ':config-trans' => (\Drupal::moduleHandler()->moduleExists('config_translation')) ? \Drupal::url('help.page', ['name' => 'config_translation']) : '#', ':language' => \Drupal::url('help.page', ['name' => 'language']), ':translation-entity' => 'https://www.drupal.org/documentation/modules/translation', ':field_help' => \Drupal::url('help.page', ['name' => 'field'])]) . '</p>';
+      $output .= '<p>' . t('The Content Translation module allows you to translate content, comments, custom blocks, taxonomy terms, users and other <a href=":field_help" title="Field module help, with background on content entities">content entities</a>. Together with the modules <a href=":language">Language</a>, <a href=":config-trans">Configuration Translation</a>, and <a href=":locale">Interface Translation</a>, it allows you to build multilingual websites. For more information, see the <a href=":translation-entity">online documentation for the Content Translation module</a>.', [':locale' => (\Drupal::moduleHandler()->moduleExists('locale')) ? Url::fromRoute('help.page', ['name' => 'locale'])->toString() : '#', ':config-trans' => (\Drupal::moduleHandler()->moduleExists('config_translation')) ? Url::fromRoute('help.page', ['name' => 'config_translation'])->toString() : '#', ':language' => Url::fromRoute('help.page', ['name' => 'language'])->toString(), ':translation-entity' => 'https://www.drupal.org/documentation/modules/translation', ':field_help' => Url::fromRoute('help.page', ['name' => 'field'])->toString()]) . '</p>';
       $output .= '<h3>' . t('Uses') . '</h3>';
       $output .= '<dl>';
       $output .= '<dt>' . t('Enabling translation') . '</dt>';
-      $output .= '<dd>' . t('In order to translate content, the website must have at least two <a href=":url">languages</a>. When that is the case, you can enable translation for the desired content entities on the <a href=":translation-entity">Content language</a> page. When enabling translation you can choose the default language for content and decide whether to show the language selection field on the content editing forms.', [':url' => \Drupal::url('entity.configurable_language.collection'), ':translation-entity' => \Drupal::url('language.content_settings_page'), ':language-help' => \Drupal::url('help.page', ['name' => 'language'])]) . '</dd>';
+      $output .= '<dd>' . t('In order to translate content, the website must have at least two <a href=":url">languages</a>. When that is the case, you can enable translation for the desired content entities on the <a href=":translation-entity">Content language</a> page. When enabling translation you can choose the default language for content and decide whether to show the language selection field on the content editing forms.', [':url' => Url::fromRoute('entity.configurable_language.collection')->toString(), ':translation-entity' => Url::fromRoute('language.content_settings_page')->toString(), ':language-help' => Url::fromRoute('help.page', ['name' => 'language'])->toString()]) . '</dd>';
       $output .= '<dt>' . t('Enabling field translation') . '</dt>';
       $output .= '<dd>' . t('You can define which fields of a content entity can be translated. For example, you might want to translate the title and body field while leaving the image field untranslated. If you exclude a field from being translated, it will still show up in the content editing form, but any changes made to that field will be applied to <em>all</em> translations of that content.') . '</dd>';
       $output .= '<dt>' . t('Translating content') . '</dt>';
@@ -44,7 +46,7 @@
     case 'language.content_settings_page':
       $output = '';
       if (!\Drupal::languageManager()->isMultilingual()) {
-        $output .= '<p>' . t('Before you can translate content, there must be at least two languages added on the <a href=":url">languages administration</a> page.', [':url' => \Drupal::url('entity.configurable_language.collection')]) . '</p>';
+        $output .= '<p>' . t('Before you can translate content, there must be at least two languages added on the <a href=":url">languages administration</a> page.', [':url' => Url::fromRoute('entity.configurable_language.collection')->toString()]) . '</p>';
       }
       return $output;
   }
@@ -170,6 +172,75 @@
 }
 
 /**
+ * Implements hook_ENTITY_TYPE_insert().
+ *
+ * Installs Content Translation's field storage definitions for the target
+ * entity type, if required.
+ *
+ * Also clears the bundle information cache so that the bundle's translatability
+ * will be set properly.
+ *
+ * @see content_translation_entity_bundle_info_alter()
+ * @see \Drupal\content_translation\ContentTranslationManager::isEnabled()
+ */
+function content_translation_language_content_settings_insert(ContentLanguageSettingsInterface $settings) {
+  if ($settings->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
+    _content_translation_install_field_storage_definitions($settings->getTargetEntityTypeId());
+  }
+
+  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
+}
+
+/**
+ * Implements hook_ENTITY_TYPE_update().
+ *
+ * Installs Content Translation's field storage definitions for the target
+ * entity type, if required.
+ *
+ * Also clears the bundle information cache so that the bundle's translatability
+ * will be changed properly.
+ *
+ * @see content_translation_entity_bundle_info_alter()
+ * @see \Drupal\content_translation\ContentTranslationManager::isEnabled()
+ */
+function content_translation_language_content_settings_update(ContentLanguageSettingsInterface $settings) {
+  $original_settings = $settings->original;
+  if ($settings->getThirdPartySetting('content_translation', 'enabled', FALSE)
+    && !$original_settings->getThirdPartySetting('content_translation', 'enabled', FALSE)
+  ) {
+    _content_translation_install_field_storage_definitions($settings->getTargetEntityTypeId());
+  }
+  \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
+}
+
+/**
+ * Installs Content Translation's fields for a given entity type.
+ *
+ * @param string $entity_type_id
+ *   The entity type ID.
+ *
+ * @todo Generalize this code in https://www.drupal.org/node/2346013.
+ */
+function _content_translation_install_field_storage_definitions($entity_type_id) {
+  /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */
+  $field_manager = \Drupal::service('entity_field.manager');
+  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema_repository */
+  $schema_repository = \Drupal::service('entity.last_installed_schema.repository');
+  $definition_update_manager = \Drupal::entityDefinitionUpdateManager();
+
+  $field_manager->useCaches(FALSE);
+  $storage_definitions = $field_manager->getFieldStorageDefinitions($entity_type_id);
+  $field_manager->useCaches(TRUE);
+  $installed_storage_definitions = $schema_repository->getLastInstalledFieldStorageDefinitions($entity_type_id);
+  foreach (array_diff_key($storage_definitions, $installed_storage_definitions) as $storage_definition) {
+    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition */
+    if ($storage_definition->getProvider() == 'content_translation') {
+      $definition_update_manager->installFieldStorageDefinition($storage_definition->getName(), $entity_type_id, 'content_translation', $storage_definition);
+    }
+  }
+}
+
+/**
  * Implements hook_entity_bundle_info_alter().
  */
 function content_translation_entity_bundle_info_alter(&$bundles) {
@@ -199,7 +270,7 @@
   $entity_type_id = $entity_type->id();
   if ($manager->isSupported($entity_type_id)) {
     $definitions = $manager->getTranslationHandler($entity_type_id)->getFieldDefinitions();
-    $installed_storage_definitions = \Drupal::entityManager()->getLastInstalledFieldStorageDefinitions($entity_type_id);
+    $installed_storage_definitions = \Drupal::service('entity.last_installed_schema.repository')->getLastInstalledFieldStorageDefinitions($entity_type_id);
     // We return metadata storage fields whenever content translation is enabled
     // or it was enabled before, so that we keep translation metadata around
     // when translation is disabled.
@@ -250,7 +321,7 @@
   if ($entity->hasLinkTemplate('drupal:content-translation-overview') && content_translation_translate_access($entity)->isAllowed()) {
     $operations['translate'] = [
       'title' => t('Translate'),
-      'url' => $entity->urlInfo('drupal:content-translation-overview'),
+      'url' => $entity->toUrl('drupal:content-translation-overview'),
       'weight' => 50,
     ];
   }
@@ -416,9 +487,9 @@
 
   // Provide helpful pointers for administrators.
   if (\Drupal::currentUser()->hasPermission('administer content translation') &&  !$bundle_is_translatable) {
-    $toggle_url = \Drupal::url('language.content_settings_page', [], [
+    $toggle_url = Url::fromRoute('language.content_settings_page', [], [
       'query' => \Drupal::destination()->getAsArray(),
-    ]);
+    ])->toString();
     $form['translatable']['#description'] = t('To configure translation for this field, <a href=":language-settings-url">enable language support</a> for this type.', [
       ':language-settings-url' => $toggle_url,
     ]);
@@ -438,19 +509,12 @@
  * Implements hook_entity_presave().
  */
 function content_translation_entity_presave(EntityInterface $entity) {
-  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && !$entity->isNew()) {
+  if ($entity instanceof ContentEntityInterface && $entity->isTranslatable() && !$entity->isNew() && isset($entity->original)) {
     /** @var \Drupal\content_translation\ContentTranslationManagerInterface $manager */
     $manager = \Drupal::service('content_translation.manager');
     if (!$manager->isEnabled($entity->getEntityTypeId(), $entity->bundle())) {
       return;
     }
-    // If we are creating a new translation we need to use the source language
-    // as original language, since source values are the only ones available to
-    // compare against.
-    if (!isset($entity->original)) {
-      $entity->original = \Drupal::entityTypeManager()
-        ->getStorage($entity->entityType())->loadUnchanged($entity->id());
-    }
     $langcode = $entity->language()->getId();
     $source_langcode = !$entity->original->hasTranslation($langcode) ? $manager->getTranslationMetadata($entity)->getSource() : NULL;
     \Drupal::service('content_translation.synchronizer')->synchronizeFields($entity, $langcode, $source_langcode);
@@ -567,7 +631,7 @@
 
   if (\Drupal::service('content_translation.manager')->isEnabled($context['entity_type'], $context['bundle']) != $enabled) {
     \Drupal::service('content_translation.manager')->setEnabled($context['entity_type'], $context['bundle'], $enabled);
-    \Drupal::entityManager()->clearCachedDefinitions();
+    \Drupal::entityTypeManager()->clearCachedDefinitions();
     \Drupal::service('router.builder')->setRebuildNeeded();
   }
 }