3 * Content Translation admin behaviors.
6 (function ($, Drupal, drupalSettings) {
8 * Forces applicable options to be checked as translatable.
10 * @type {Drupal~behavior}
12 * @prop {Drupal~behaviorAttach} attach
13 * Attaches content translation dependent options to the UI.
15 Drupal.behaviors.contentTranslationDependentOptions = {
17 const $context = $(context);
18 const options = drupalSettings.contentTranslationDependentOptions;
21 function fieldsChangeHandler($fields, dependentColumns) {
23 Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependentColumns, $(e.target));
27 // We're given a generic name to look for so we find all inputs containing
28 // that name and copy over the input values that require all columns to be
30 if (options && options.dependent_selectors) {
31 Object.keys(options.dependent_selectors).forEach((field) => {
32 $fields = $context.find(`input[name^="${field}"]`);
33 const dependentColumns = options.dependent_selectors[field];
35 $fields.on('change', fieldsChangeHandler($fields, dependentColumns));
36 Drupal.behaviors.contentTranslationDependentOptions.check($fields, dependentColumns);
40 check($fields, dependentColumns, $changed) {
41 let $element = $changed;
44 function filterFieldsList(index, field) {
45 return $(field).val() === column;
48 // A field that has many different translatable parts can also define one
49 // or more columns that require all columns to be translatable.
50 Object.keys(dependentColumns || {}).forEach((index) => {
51 column = dependentColumns[index];
54 $element = $fields.filter(filterFieldsList);
57 if ($element.is(`input[value="${column}"]:checked`)) {
58 $fields.prop('checked', true)
59 .not($element).prop('disabled', true);
62 $fields.prop('disabled', false);
69 * Makes field translatability inherit bundle translatability.
71 * @type {Drupal~behavior}
73 * @prop {Drupal~behaviorAttach} attach
74 * Attaches content translation behavior.
76 Drupal.behaviors.contentTranslation = {
78 // Initially hide all field rows for non translatable bundles and all
79 // column rows for non translatable fields.
80 $(context).find('table .bundle-settings .translatable :input').once('translation-entity-admin-hide').each(function () {
81 const $input = $(this);
82 const $bundleSettings = $input.closest('.bundle-settings');
83 if (!$input.is(':checked')) {
84 $bundleSettings.nextUntil('.bundle-settings').hide();
88 .nextUntil('.bundle-settings', '.field-settings')
89 .find('.translatable :input:not(:checked)')
90 .closest('.field-settings')
91 .nextUntil(':not(.column-settings)')
96 // When a bundle is made translatable all of its fields should inherit
97 // this setting. Instead when it is made non translatable its fields are
98 // hidden, since their translatability no longer matters.
99 $('body').once('translation-entity-admin-bind').on('click', 'table .bundle-settings .translatable :input', (e) => {
100 const $target = $(e.target);
101 const $bundleSettings = $target.closest('.bundle-settings');
102 const $settings = $bundleSettings.nextUntil('.bundle-settings');
103 const $fieldSettings = $settings.filter('.field-settings');
104 if ($target.is(':checked')) {
105 $bundleSettings.find('.operations :input[name$="[language_alterable]"]').prop('checked', true);
106 $fieldSettings.find('.translatable :input').prop('checked', true);
113 .on('click', 'table .field-settings .translatable :input', (e) => {
114 const $target = $(e.target);
115 const $fieldSettings = $target.closest('.field-settings');
116 const $columnSettings = $fieldSettings.nextUntil('.field-settings, .bundle-settings');
117 if ($target.is(':checked')) {
118 $columnSettings.show();
121 $columnSettings.hide();
126 }(jQuery, Drupal, drupalSettings));