Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Got '[object Object]' as initial machine name when:
- the field label has a default value that must to be transliterated (contains spaces, etc.)
- there are no 'exists' callback, or it is not valid
As an exists callback is commonly used, I mark it with minor priority.
I found a bug in machine-name.es6.js (line 116) that produces this behaviour.
File: core/misc/machine-name.es6.js
...
109 // Determine the initial machine name value. Unless the machine name
110 // form element is disabled or not empty, the initial default value is
111 // based on the human-readable form element value.
112 if ($target.is(':disabled') || $target.val() !== '') {
113 machine = $target.val();
114 }
115 else if ($source.val() !== '') {
116 machine = self.transliterate($source.val(), options);
117 }
...
self.transliterate returns an object, not a string. At the same file:
...
transliterate(source, settings) {
return $.get(Drupal.url('machine_name/transliterate'), {
text: source,
...
Sample form to reproduce
$form['label'] = [
'#type' => 'textfield',
'#title' => t('Test label'),
'#default_value' => 'Custom field label test',
'#size' => 15,
'#required' => TRUE,
];
$form['machine'] = [
'#type' => 'machine_name',
'#size' => 15,
'#description' => t('A unique machine-readable name containing letters, numbers, and underscores.'),
'#required' => FALSE,
'#maxlength' => 32,
'#machine_name' => [
'source' => [
'label',
],
],
];
Proposed resolution
There is an issue that proposes to refactor the machine-name.es6.js: #1686174: Refactor machine-name.js. I did not test it but probably this bug is not present in the new code. However should be good to solve on current code in the meantime.
Remaining tasks
- Write a patch that fixes the bug.
- Review #1686174: Refactor machine-name.js to check if it solves this issue.
Comment | File | Size | Author |
---|---|---|---|
#22 | 2942663-18-drupal-9.0.x.patch | 6.16 KB | lauriii |
#21 | after.png | 75.74 KB | jungle |
#21 | before.png | 78.28 KB | jungle |
#18 | interdiff-2942663-13-18.txt | 1.75 KB | manuel.adan |
#18 | 2942663-18.patch | 5.93 KB | manuel.adan |
Comments
Comment #2
manuel.adanComment #3
andypostComment #4
GrandmaGlassesRopeMan- cleans up comment format
- fixes loose equal
- fixes unnamed function (should be arrow anyways)
- fixes duplicate variable name in scope
Comment #5
samuel.mortensonHere's some test coverage.
Comment #7
tedbowShould we make this default use a comma or something to make sure it has a more complex value to convert for testing?
Otherwise looks great!
Comment #8
manuel.adan#5 works for me, I've tested it on transaction module, where the bug is really noticeable, as shown in the attached after / before screenshots
Comment #9
tedbowRe comment in #7. I realize that we already have specific tests for this
\Drupal\Tests\system\Unit\Transliteration\MachineNameControllerTest
So what is there now is fine.
Comment #10
Gábor HojtsyThe code comments got mixed up somehow(?)
What does this added line add to the docs?
We are not actually appending anything here, that happens below and already has that comment.
Comment #11
manuel.adanComments reviewed. Also initial value assignment according to the updated logic.
Comment #13
manuel.adanPorted to latest 8.7.x-dev after #2981652: [Aug. 9 2018] Format core JavaScript using recently add Prettier
Comment #14
jmsosso CreditAttribution: jmsosso at servinube commentedComment #15
jmsosso CreditAttribution: jmsosso at servinube commentedAlso I saw a weird behavior, with or without the patch. Two AJAX request to machine_name/transliterate are fired for the same field. Because of #1686174 I didn't go deep into it.
The bug I saw in the original machine-name.es6.js is that, in line 126, the promise returned by trasliterate() is directly used as if it is the new machine name, but it isn't. It is an object.
But that is fixed with the patch.
Comment #17
lauriiiThere are some prettier and eslint violations that should be fixed in the patch.
Also, according to our coding standards, comma should follow the last multiline array item.
Comment #18
manuel.adanCS and prettier review.
Comment #21
jungleConfirm the patch #18 fixed the known issue in transaction module: Machine name in field creation form takes an initial value of "[Object object]".
Drupal core: 8.8.1
Before:
After:
Comment #22
lauriiiHere's also a Drupal 9.0.x version of the patch.
Comment #23
alexpottCommitted c9c542f and pushed to 9.0.x. Thanks!
Committed 847f9ec and pushed to 8.9.x. Thanks!
Didn't backport to 8.8.x because this bug is very minor and changes javascript which is not the best tested part of our ecosystem. So this gives us a chance to find out if this introduces anything unexpected.