From 4bc5aa838029337aa7c6b5b6deaf0d46c98df2f4 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Tue, 28 Apr 2015 03:53:05 -0500
Subject: [PATCH] Issue #2449457 by Anushka-mp, sasanikolic, Berdir, plach:
 inconsistent checks in content_translation

---
 .../content_translation.admin.inc             |  6 ++-
 .../content_translation.module                |  8 ++++
 .../src/ContentTranslationManager.php         |  2 +-
 .../Tests/ContentTranslationUISkipTest.php    | 44 +++++++++++++++++++
 .../content_translation_test.info.yml         | 10 +++++
 .../Entity/EntityTestTranslatableNoUISkip.php | 32 ++++++++++++++
 .../Entity/EntityTestTranslatableUISkip.php   | 33 ++++++++++++++
 7 files changed, 132 insertions(+), 3 deletions(-)
 create mode 100644 core/modules/content_translation/src/Tests/ContentTranslationUISkipTest.php
 create mode 100644 core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.info.yml
 create mode 100644 core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableNoUISkip.php
 create mode 100644 core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableUISkip.php

diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc
index 2f3bc0a22dfc..af5c2c4946d5 100644
--- a/core/modules/content_translation/content_translation.admin.inc
+++ b/core/modules/content_translation/content_translation.admin.inc
@@ -87,13 +87,15 @@ function _content_translation_form_language_content_settings_form_alter(array &$
     $entity_type = $entity_manager->getDefinition($entity_type_id);
     $storage_definitions = $entity_type instanceof ContentEntityTypeInterface ? $entity_manager->getFieldStorageDefinitions($entity_type_id) : array();
 
-    $entity_type_translatable = $entity_type->isTranslatable();
+    $entity_type_translatable = $content_translation_manager->isSupported($entity_type_id);
     foreach (entity_get_bundles($entity_type_id) as $bundle => $bundle_info) {
       // Here we do not want the widget to be altered and hold also the "Enable
       // translation" checkbox, which would be redundant. Hence we add this key
-      // to be able to skip alterations.
+      // to be able to skip alterations. Alter the title and display the message
+      // about UI integration.
       $form['settings'][$entity_type_id][$bundle]['settings']['language']['#content_translation_skip_alter'] = TRUE;
       if (!$entity_type_translatable) {
+        $form['settings'][$entity_type_id]['#title'] = t('@label (Translation is not supported).', array('@label' => $entity_type->getLabel()));
         continue;
       }
 
diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module
index 9ab0bfd6cae7..2751862d7751 100644
--- a/core/modules/content_translation/content_translation.module
+++ b/core/modules/content_translation/content_translation.module
@@ -92,6 +92,14 @@ function content_translation_language_types_info_alter(array &$language_types) {
  * assumed. Every translation handler must implement
  * \Drupal\content_translation\ContentTranslationHandlerInterface.
  *
+ * By default, entity types that do not have a canonical link template cannot be
+ * enabled for translation. This can be overridden by setting the
+ * 'content_translation_ui_skip' key to true. When that key is set, the Content
+ * Translation module will not provide any UI for translating the entity type,
+ * and the entity type should implement its own UI. This is useful for (e.g.)
+ * entity types that are embedded into others for editing (which would not need
+ * a canonical link, but could still support translation).
+ *
  * To implement its business logic the content translation UI relies on various
  * metadata items describing the translation state. The default implementation
  * is provided by \Drupal\content_translation\ContentTranslationMetadataWrapper,
diff --git a/core/modules/content_translation/src/ContentTranslationManager.php b/core/modules/content_translation/src/ContentTranslationManager.php
index 11c93dab742e..d0525e192cf5 100644
--- a/core/modules/content_translation/src/ContentTranslationManager.php
+++ b/core/modules/content_translation/src/ContentTranslationManager.php
@@ -66,7 +66,7 @@ public function getTranslationMetadata(EntityInterface $translation) {
    */
   public function isSupported($entity_type_id) {
     $entity_type = $this->entityManager->getDefinition($entity_type_id);
-    return $entity_type->isTranslatable() && $entity_type->hasLinkTemplate('drupal:content-translation-overview');
+    return $entity_type->isTranslatable() && ($entity_type->hasLinkTemplate('drupal:content-translation-overview') || $entity_type->get('content_translation_ui_skip'));
   }
 
   /**
diff --git a/core/modules/content_translation/src/Tests/ContentTranslationUISkipTest.php b/core/modules/content_translation/src/Tests/ContentTranslationUISkipTest.php
new file mode 100644
index 000000000000..d04be44fa80e
--- /dev/null
+++ b/core/modules/content_translation/src/Tests/ContentTranslationUISkipTest.php
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\content_translation\Tests\ContentTranslationUISkipTest.
+ */
+
+namespace Drupal\content_translation\Tests;
+
+use Drupal\simpletest\WebTestBase;
+
+/**
+ * Tests the content translation UI check skip.
+ *
+ * @group content_translation
+ */
+class ContentTranslationUISkipTest extends WebTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('content_translation_test', 'user', 'node');
+
+  /**
+   * Tests the content_translation_ui_skip key functionality.
+   */
+  function testUICheckSkip() {
+    $admin_user = $this->drupalCreateUser(array(
+      'translate any entity',
+      'administer content translation',
+      'administer languages'
+    ));
+    $this->drupalLogin($admin_user);
+    // Visit the content translation.
+    $this->drupalGet('admin/config/regional/content-language');
+
+    // Check the message regarding UI integration.
+    $this->assertText('Test entity - Translatable skip UI check');
+    $this->assertText('Test entity - Translatable check UI (Translation is not supported)');
+  }
+
+}
diff --git a/core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.info.yml b/core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.info.yml
new file mode 100644
index 000000000000..83ec8b8e62ed
--- /dev/null
+++ b/core/modules/content_translation/tests/modules/content_translation_test/content_translation_test.info.yml
@@ -0,0 +1,10 @@
+name: 'Content translation tests'
+type: module
+description: 'Provides content translation tests.'
+package: Testing
+version: VERSION
+core: 8.x
+dependencies:
+  - content_translation
+  - language
+  - entity_test
diff --git a/core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableNoUISkip.php b/core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableNoUISkip.php
new file mode 100644
index 000000000000..0c093229e6e0
--- /dev/null
+++ b/core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableNoUISkip.php
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\content_translation_test\Entity\EntityTestTranslatableNoUISkip.
+ */
+
+namespace Drupal\content_translation_test\Entity;
+
+use Drupal\entity_test\Entity\EntityTest;
+
+/**
+ * Defines the test entity class.
+ *
+ * @ContentEntityType(
+ *   id = "entity_test_translatable_no_skip",
+ *   label = @Translation("Test entity - Translatable check UI"),
+ *   base_table = "entity_test_mul",
+ *   data_table = "entity_test_mul_property_data",
+ *   entity_keys = {
+ *     "id" = "id",
+ *     "uuid" = "uuid",
+ *     "bundle" = "type",
+ *     "label" = "name",
+ *     "langcode" = "langcode",
+ *   },
+ *   translatable = TRUE,
+ * )
+ */
+class EntityTestTranslatableNoUISkip extends EntityTest {
+
+}
diff --git a/core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableUISkip.php b/core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableUISkip.php
new file mode 100644
index 000000000000..e8ba75183d4b
--- /dev/null
+++ b/core/modules/content_translation/tests/modules/content_translation_test/src/Entity/EntityTestTranslatableUISkip.php
@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * @file
+ * Contains Drupal\content_translation_test\Entity\EntityTestTranslatableUISkip.
+ */
+
+namespace Drupal\content_translation_test\Entity;
+
+use Drupal\entity_test\Entity\EntityTest;
+
+/**
+ * Defines the test entity class.
+ *
+ * @ContentEntityType(
+ *   id = "entity_test_translatable_UI_skip",
+ *   label = @Translation("Test entity - Translatable skip UI check"),
+ *   base_table = "entity_test_mul",
+ *   data_table = "entity_test_mul_property_data",
+ *   entity_keys = {
+ *     "id" = "id",
+ *     "uuid" = "uuid",
+ *     "bundle" = "type",
+ *     "label" = "name",
+ *     "langcode" = "langcode",
+ *   },
+ *   translatable = TRUE,
+ *   content_translation_ui_skip = TRUE,
+ * )
+ */
+class EntityTestTranslatableUISkip extends EntityTest {
+
+}
-- 
GitLab