diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc index d2a3e1e..d18fca1 100644 --- a/core/modules/content_translation/content_translation.admin.inc +++ b/core/modules/content_translation/content_translation.admin.inc @@ -15,17 +15,20 @@ /** * 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) { $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(); @@ -35,12 +38,19 @@ function content_translation_field_sync_widget(FieldDefinitionInterface $field) $default[$group] = !empty($info['translatable']) ? $group : FALSE; } - $settings = array('dependent_selectors' => array('instance[settings][translation_sync]' => array('file'))); + $settings = array( + 'dependent_selectors' => array( + 'instance[settings][translation_sync]' => array('file'), + ), + 'translation_sync_options' => array( + 'instance[settings][translation_sync]' => array_keys($options), + ), + ); - $translation_sync = $field->getSetting('translation_sync'); $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' => !empty($translation_sync) ? $translation_sync : $default, '#attached' => array( @@ -51,6 +61,7 @@ function content_translation_field_sync_widget(FieldDefinitionInterface $field) array('data' => array('contentTranslationDependentOptions' => $settings), 'type' => 'setting'), ), ), + '#weight' => 15, ); } diff --git a/core/modules/content_translation/content_translation.admin.js b/core/modules/content_translation/content_translation.admin.js index 13a752f..addc19c 100644 --- a/core/modules/content_translation/content_translation.admin.js +++ b/core/modules/content_translation/content_translation.admin.js @@ -9,7 +9,7 @@ attach: function (context) { var $context = $(context); var options = drupalSettings.contentTranslationDependentOptions; - var $fields, dependent_columns; + var $fields, $fields_required, $translatable_field, column, dependent_columns, translation_sync_options; function fieldsChangeHandler($fields, dependent_columns) { return function (e) { @@ -17,6 +17,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. @@ -31,6 +37,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; @@ -109,6 +143,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); + } + } } };