From 1d59ece6316d3c69f85207a7516e074a46fd8a3a Mon Sep 17 00:00:00 2001
From: M Parker <mparker17@536298.no-reply.drupal.org>
Date: Tue, 25 Aug 2015 13:21:12 -0400
Subject: [PATCH] 1920876-83

---
 .../content_translation.admin.inc                  | 27 +++++++---
 .../content_translation.admin.js                   | 59 +++++++++++++++++++++-
 2 files changed, 77 insertions(+), 9 deletions(-)

diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc
index 5623f75..52eb4a9 100644
--- a/core/modules/content_translation/content_translation.admin.inc
+++ b/core/modules/content_translation/content_translation.admin.inc
@@ -18,21 +18,25 @@
 /**
  * Returns a form element to configure field synchronization.
  *
- * @param \Drupal\Core\Field\FieldDefinitionInterface $field
- *   A field definition object.
+ * @param \Drupal\Core\Field\FieldInstanceConfigInterface $field_instance
+ *   Instance config object.
  *
  * @return array
  *   A form element to configure field synchronization.
  */
-function content_translation_field_sync_widget(FieldDefinitionInterface $field) {
+function content_translation_field_sync_widget(FieldDefinitionInterface $field_instance) {
   // No way to store field sync information on this field.
-  if (!($field instanceof ThirdPartySettingsInterface)) {
+  if (!($field_instance instanceof ThirdPartySettingsInterface)) {
     return array();
   }
 
   $element = array();
-  $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field->getType());
+
+  $translation_sync = $field_instance->getSetting('translation_sync');
+
+  $definition = \Drupal::service('plugin.manager.field.field_type')->getDefinition($field_instance->getType());
   $column_groups = $definition['column_groups'];
+
   if (!empty($column_groups) && count($column_groups) > 1) {
     $options = array();
     $default = array();
@@ -42,12 +46,20 @@ function content_translation_field_sync_widget(FieldDefinitionInterface $field)
       $default[$group] = !empty($info['translatable']) ? $group : FALSE;
     }
 
-    $settings = array('dependent_selectors' => array('instance[third_party_settings][content_translation][translation_sync]' => array('file')));
-    $default = $field->getThirdPartySetting('content_translation', 'translation_sync', $default);
+    $settings = array(
+      'dependent_selectors' => array(
+        'instance[third_party_settings][content_translation][translation_sync]' => array('file'),
+      ),
+      'translation_sync_options' => array(
+        'instance[third_party_settings][content_translation][translation_sync]' => array_keys($options),
+      ),
+    );
+    $default = $field_instance->getThirdPartySetting('content_translation', 'translation_sync', $default);
 
     $element = array(
       '#type' => 'checkboxes',
       '#title' => t('Translatable elements'),
+      '#description' => t('Disabled fields are not shown. Please first enable the fields you need to make translatable.'),
       '#options' => $options,
       '#default_value' => $default,
       '#attached' => array(
@@ -58,6 +70,7 @@ function content_translation_field_sync_widget(FieldDefinitionInterface $field)
           'contentTranslationDependentOptions' => $settings,
         ],
       ),
+      '#weight' => 15,
     );
   }
 
diff --git a/core/modules/content_translation/content_translation.admin.js b/core/modules/content_translation/content_translation.admin.js
index 0071b6e..d0e0e8d 100644
--- a/core/modules/content_translation/content_translation.admin.js
+++ b/core/modules/content_translation/content_translation.admin.js
@@ -16,8 +16,7 @@
     attach: function (context) {
       var $context = $(context);
       var options = drupalSettings.contentTranslationDependentOptions;
-      var $fields;
-      var dependent_columns;
+      var $fields, $fields_required, $translatable_field, column, dependent_columns, translation_sync_options;
 
       function fieldsChangeHandler($fields, dependent_columns) {
         return function (e) {
@@ -25,6 +24,12 @@
         };
       }
 
+      function translationHideDisabled($translatable_fields, column) {
+        return function (e) {
+          Drupal.behaviors.contentTranslation.check($translatable_fields, column, $(e.target));
+        };
+      }
+
       // We're given a generic name to look for so we find all inputs containing
       // that name and copy over the input values that require all columns to be
       // translatable.
@@ -39,6 +44,34 @@
           }
         }
       }
+
+      // Hides/Show translation checkboxes for disabled field values.
+      if (options.translation_sync_options) {
+        for (var field2 in options.translation_sync_options) {
+          if (options.translation_sync_options.hasOwnProperty(field2)) {
+            translation_sync_options = options.translation_sync_options[field2];
+
+            for (var index in translation_sync_options) {
+              if (translation_sync_options.hasOwnProperty(index)) {
+                column = translation_sync_options[index];
+                $translatable_field = $context.find('input[name^="' + field2 + '[' + column + ']"]');
+
+                // Normal fields.
+                $fields = $context.find('input[name^="instance[settings][' + column + '_field]"]');
+
+                $fields.on('change', translationHideDisabled($translatable_field, column));
+
+                // Required fields.
+                $fields_required = $context.find('input[name^="instance[settings][' + column + '_field_required]"]');
+
+                $fields_required.on('change', translationHideDisabled($translatable_field, column));
+
+                Drupal.behaviors.contentTranslation.check($translatable_field, column);
+              }
+            }
+          }
+        }
+      }
     },
     check: function ($fields, dependent_columns, $changed) {
       var $element = $changed;
@@ -119,6 +152,28 @@
             $columnSettings.hide();
           }
         });
+    },
+    check: function ($translatable_field, column, $changed) {
+      var $target = $('input[name^="instance[settings][' + column + '_field]"]'),
+        $target_required = $('input[name^="instance[settings][' + column + '_field_required]"]'),
+        base_state;
+
+      base_state = $translatable_field.prop('checked');
+
+      if ($target.is(':checked')) {
+        if ($target_required.is(':checked')) {
+          base_state = true;
+        }
+
+        $translatable_field.prop('checked', base_state).closest('.form-item').show();
+      }
+      else {
+          if($target.length > 0) {
+            $translatable_field.prop('checked', base_state).closest('.form-item').hide();
+          } else {
+            $translatable_field.prop('checked', base_state);
+          }
+      }
     }
   };
 
-- 
2.5.0

