diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc index 8ea5d1c..a0451f1 100644 --- a/core/modules/content_translation/content_translation.admin.inc +++ b/core/modules/content_translation/content_translation.admin.inc @@ -33,12 +33,16 @@ 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_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( @@ -50,6 +54,13 @@ function content_translation_field_sync_widget(FieldDefinitionInterface $field) ), ), ); + // Check if the Title and Alt may be translated and move the form element + // after these ones. + if (array_key_exists('title', $element['#default_value']) && array_key_exists('alt', $element['#default_value'])) { + // Special case for image fields. Move the Translatable elements towards + // the bottom, after the enable/disable controls. + $element['#weight'] = 13; + } } return $element; @@ -110,6 +121,19 @@ function _content_translation_form_language_content_settings_form_alter(array &$ ); $column_element = content_translation_field_sync_widget($definition); if ($column_element) { + // Hide column items that are disabled for this field instance. + foreach (array_keys($column_element['#options']) as $column_item) { + if (isset($instance->definition['settings'][$column_item])) { + if (!$instance->definition['settings'][$column_item]) { + unset($column_element['#options'][$column_item]); + } + } + else if (isset($instance->definition['settings'][$column_item . '_field'])) { + if (!$instance->definition['settings'][$column_item . '_field']) { + unset($column_element['#options'][$column_item]); + } + } + } $form['settings'][$entity_type_id][$bundle]['columns'][$field_name] = $column_element; // @todo This should not concern only files. diff --git a/core/modules/content_translation/content_translation.admin.js b/core/modules/content_translation/content_translation.admin.js index 13a752f..f5f4a8f 100644 --- a/core/modules/content_translation/content_translation.admin.js +++ b/core/modules/content_translation/content_translation.admin.js @@ -112,4 +112,43 @@ } }; + /** + * Hides translation checkboxes for disabled field values. + */ + Drupal.behaviors.translationHideDisabled = { + attach: function (context, settings) { + $.each(settings.contentTranslationDependentOptions.translation_sync_options, function (index, element) { + // Get the translatable element checkbox. + var $translatableCheckbox = $(':input[name="instance[settings][translation_sync][' + element + ']"][type="checkbox"]'); + // Try to guess the checkbox that enables the field. + var $enableCheckbox = $(':input[name="instance[settings][' + element + ']"][type="checkbox"]'); + if (!$enableCheckbox.length) { + // If the setting did not exist try adding _field. + $enableCheckbox = $(':input[name="instance[settings][' + element + '_field]"][type="checkbox"]'); + } + ; + if ($enableCheckbox.length) { + // If the field is not enabled then uncheck and disable the translation + // checkbox. + var uncheckHideCheckboxes = function ($checkbox) { + if (!$checkbox.is(':checked')) { + $translatableCheckbox.prop('checked', false); + // Finds the wrapper form item and hide it. + $translatableCheckbox.closest('.form-item').hide(); + } + else { + // Finds the wrapper form item and shows it. + $translatableCheckbox.closest('.form-item').show(); + } + }; + // Bind on change. + $enableCheckbox.on('change', function (e) { + uncheckHideCheckboxes($(e.target)); + }); + // Process first load. + uncheckHideCheckboxes($enableCheckbox); + } + }); + } + }; })(jQuery, Drupal, drupalSettings);