diff options
4 files changed, 41 insertions, 18 deletions
diff --git a/core/misc/machine-name.es6.js b/core/misc/machine-name.es6.js index 0fee7f6b5f7..77339d53cde 100644 --- a/core/misc/machine-name.es6.js +++ b/core/misc/machine-name.es6.js @@ -88,7 +88,6 @@ } Object.keys(settings.machineName).forEach(sourceId => { - let machine = ''; const options = settings.machineName[sourceId]; const $source = $context @@ -117,14 +116,8 @@ options.maxlength = $target.attr('maxlength'); // Hide the form item container of the machine name form element. $wrapper.addClass('visually-hidden'); - // Determine the initial machine name value. Unless the machine name - // form element is disabled or not empty, the initial default value is - // based on the human-readable form element value. - if ($target.is(':disabled') || $target.val() !== '') { - machine = $target.val(); - } else if ($source.val() !== '') { - machine = self.transliterate($source.val(), options); - } + // Initial machine name from the target field default value. + const machine = $target.val(); // Append the machine name preview to the source field. const $preview = $( `<span class="machine-name-value">${ @@ -152,6 +145,18 @@ $preview, options, }; + + // If no initial value, determine machine name based on the + // human-readable form element value. + if (machine === '' && $source.val() !== '') { + self.transliterate($source.val(), options).done(machineName => { + self.showMachineName( + machineName.substr(0, options.maxlength), + eventData, + ); + }); + } + // If it is editable, append an edit link. const $link = $( `<span class="admin-link"><button type="button" class="link">${Drupal.t( diff --git a/core/misc/machine-name.js b/core/misc/machine-name.js index c065e70a863..a711a88eefc 100644 --- a/core/misc/machine-name.js +++ b/core/misc/machine-name.js @@ -50,7 +50,6 @@ } Object.keys(settings.machineName).forEach(function (sourceId) { - var machine = ''; var options = settings.machineName[sourceId]; var $source = $context.find(sourceId).addClass('machine-name-source').once('machine-name'); var $target = $context.find(options.target).addClass('machine-name-target'); @@ -67,13 +66,7 @@ options.maxlength = $target.attr('maxlength'); $wrapper.addClass('visually-hidden'); - - if ($target.is(':disabled') || $target.val() !== '') { - machine = $target.val(); - } else if ($source.val() !== '') { - machine = self.transliterate($source.val(), options); - } - + var machine = $target.val(); var $preview = $("<span class=\"machine-name-value\">".concat(options.field_prefix).concat(Drupal.checkPlain(machine)).concat(options.field_suffix, "</span>")); $suffix.empty(); @@ -95,6 +88,13 @@ $preview: $preview, options: options }; + + if (machine === '' && $source.val() !== '') { + self.transliterate($source.val(), options).done(function (machineName) { + self.showMachineName(machineName.substr(0, options.maxlength), eventData); + }); + } + var $link = $("<span class=\"admin-link\"><button type=\"button\" class=\"link\">".concat(Drupal.t('Edit'), "</button></span>")).on('click', eventData, clickEditHandler); $suffix.append($link); diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameForm.php index 121cd31aea3..3ab45f0ec44 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestMachineNameForm.php @@ -48,6 +48,19 @@ class FormTestMachineNameForm extends FormBase { 'source' => ['machine_name_2_label'], ], ]; + $form['machine_name_3_label'] = [ + '#type' => 'textfield', + '#title' => 'Machine name 3 label', + '#default_value' => 'Yet another machine name', + ]; + $form['machine_name_3'] = [ + '#type' => 'machine_name', + '#title' => 'Machine name 3', + '#description' => 'Another machine name.', + '#machine_name' => [ + 'source' => ['machine_name_3_label'], + ], + ]; $form['submit'] = [ '#type' => 'submit', '#value' => 'Submit', diff --git a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php index aaa9440e75b..3e25c97a7b9 100644 --- a/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Core/MachineNameTest.php @@ -73,11 +73,16 @@ class MachineNameTest extends WebDriverTestBase { $machine_name_2_wrapper = $machine_name_2_field->getParent(); $machine_name_1_value = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix .machine-name-value'); $machine_name_2_value = $page->find('css', '#edit-machine-name-2-label-machine-name-suffix .machine-name-value'); + $machine_name_3_value = $page->find('css', '#edit-machine-name-3-label-machine-name-suffix .machine-name-value'); $button_1 = $page->find('css', '#edit-machine-name-1-label-machine-name-suffix button.link'); - // Assert both fields are initialized correctly. + // Assert all fields are initialized correctly. $this->assertNotEmpty($machine_name_1_value, 'Machine name field 1 must be initialized'); $this->assertNotEmpty($machine_name_2_value, 'Machine name field 2 must be initialized'); + $this->assertNotEmpty($machine_name_3_value, 'Machine name field 3 must be initialized'); + + // Assert that a machine name based on a default value is initialized. + $this->assertJsCondition('jQuery("#edit-machine-name-3-label-machine-name-suffix .machine-name-value").html() == "yet_another_machine_name"'); // Field must be present for the rest of the test to work. if (empty($machine_name_1_value)) { |