Problem/Motivation
When adding a new shortcut set, the description on the machine name field uses the machine name element default ("A unique machine-readable name. Can only contain lowercase letters, numbers, and underscores."), but Shortcut machine names require hyphens. The machine name element validator checks the 'replace' key in the form element for '-' vs '_' and changes the validation text accordingly, but the description doesn't change accordingly (see attached screenshot). To reproduce, go to /admin/config/user-interface/shortcut/add-set and click the edit link by machine name after entering a Set name.
Steps to reproduce
- Visit admin/config/user-interface/shortcut/add-set
- Enter some text into the "Set name" element so the JS executes and starts auto-filling the machine name.
- Click the machine name element's Edit link.
- View the machine name form element's description.
Proposed resolution
- Change the machine_name Element's docs to tell developers that if they override the default replace_pattern, then they also need to override the default description.
- Override the ShortcutSetForm's machine_name element description to specify that hyphens are required instead of underscores.
Remaining tasks
Review.
User interface changes
The machine name element's summary on admin/config/user-interface/shortcut/add-set will be updated.
Before

After

Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3052479
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
hart0554 commentedPatch attached
Comment #3
anantjain60 commentedChanging issue status to "Need Review" since the patch is added
Comment #7
abhijith s commentedFixed the CS issues in patch #2.
Comment #9
bhumikavarshney commentedHi @Abhijith S ,
By apply this patch Machine Name element description updated with hypens.
Thanks
Comment #10
immaculatexavier commentedReviewed whether the old patch still applies to the latest code and passes automated tests.
Steps taken to review:
Downloaded the latest patch file.
Used Git to clone local Git repository of 9.2.x and 10.0.x of the development version that the issue pertains to.
The patch #7 applied successfully.
The automated tests for the project were re-run after I clicked the "retest" link in the patch file, the automated test passed successfully.
Result:
Verified the patch applied successfully, and the automated test passed successfully.
Comment #11
immaculatexavier commentedComment #12
longwaveThe words "hyphens" and "underscores" are not translatable in this patch. It might be simpler to have two variants of the full help text instead of trying to insert the correct word.
Comment #13
andregp commentedI changed the code to have two variants of the entire sentence.
Comment #14
Aline Teixeira Ramos commentedHey, I can review it. =)
Comment #15
Aline Teixeira Ramos commentedI applied the path #13 and it follows what is requested in the problem description, no errors on coding standard and also has the two variants of the help text, as requested. In my view, it's done.
Comment #16
catchThe translation parser isn't able to pick up t($description). What you can do is call do $description = t('A unique...'); and then '#description' => $description.
Also can the '==' be '=== ' here?
Comment #17
pooja saraah commentedAddressed the comment #16
Attached the patch and interdiff
Comment #18
pooja saraah commentedComment #19
asha nair commentedApplied patch #17 successfully and it changes the description to hyphens. Adding screenshots for reference
Comment #20
aarti zikre commentedverified this patch on Drupal 10.0.x.
https://www.drupal.org/files/issues/2022-07-13/3052479-17.patch
Testing Steps:
* created new instance of Drupal 10.0.x.
* go to /admin/config/user-interface/shortcut/add-set
* created shortcut set.
* view the machine name. (By default machine name convert white space to hyphen)
For example :
Name: test 1
Machine name: test-1
Test Result:
Machine name description line change from underscore to hyphen.
Refer SS
Before:
After:
Test result pass
Can be move to RTBC
Comment #21
aarti zikre commentedComment #22
alexpottI think we're doing this in the wrong place. The allowed characters are specified by the [#machine_name][replace_pattern] setting. What blocks does is this:
Imo \Drupal\shortcut\ShortcutSetForm::form should set an appropriate description and be done. We should document on \Drupal\Core\Render\Element\MachineName that if you customise the replace_pattern you should override the #description.
Comment #23
alexpottOr to put the previous comment another way the $element['#machine_name']['replace'] in no way determines the allowed characters... that's down to replace_pattern and I don't think we want to build a regex parser that turns a regex into a human readable message of what characters are allowed.
Comment #24
pflora commentedI've done the changes requested in #22 and created a new patch. The machine name description now mentions hyphens instead of underscores. If there is anything that needs to be changed, please feel free to give me any kind of feedback!
Comment #25
alexpottWe need a space after the fullstop and it should say that when this is changed the description should be updated accordingly. So
When using a different pattern, the machine name element's '#description' should be updated accordingly.Comment #26
ravi.shankar commentedMade changes as per comment #25.
Comment #27
mpauloThe changes seem to be in order with #25.
+1 to RTBC.
Comment #28
pflora commentedTested patch from #26 on drupal 10.0.x and everything seems to be working as intended.
I was able to verify that the description has changed to "A unique machine-readable name. Can only contain lowercase letters, numbers, and hyphens." and the machine name follows this pattern.
Moving this issue to RTBC since the request from #25 was adressed.
Comment #29
alexpottI'm unsure of what to do with the screenshot credit for #9, #19 and #20. #20 is a good screenshot comment and the other screenshot comments whilst could have had more comment but the screenshots all came at a useful time. So I'm going to leave the credit.
Comment #30
alexpottThis should be a translatable string. We should use
$this->t().Comment #31
asad_ahmed commentedMade changes as per #30, Thanks
Comment #32
smustgrave commentedThis issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.
As a bug it will need a test case.
Comment #36
dcam commentedI converted #31 to an MR and added an assertion for the updated machine name element description.
Comment #37
smustgrave commentedWon’t this require updating machine_name schema now too?
Just reading the proposed solution “check replacement key…” not sure that’s been implemented.
Title made me think that hyphens are valid machine names and is that true? What will that break
Comment #39
prashant.cFor this we only need to replace
-with_in thereplace_patternandreplacekeys of themachine_nameelement of thecore/modules/shortcut/src/ShortcutSetForm.php.This will allow lowercase letters, numbers, and underscores, and the "replace" key will replace other special characters with
_.Please review.
Comment #40
smustgrave commentedShould be a test around that change. Probably can drop the test for the description
Comment #41
smustgrave commentedAlso I’m not sure hyphen is a valid machine character
Comment #42
prashant.cWill try to add test coverage, if hyphen(-) is used in the machine name, it will get replaced by underscore(_).
Comment #43
dcam commentedThe issue summary explicitly says "Shortcut machine names require hyphens." If this is being undone, then justification for that needs to be given and the IS should be updated accordingly. Please ensure that proper research into this change has been performed.
Comment #44
dcam commentedThe IS needs to be updated. I forgot to do it. In #22 @alexpott outlined the solution: any element that overrides the replacement pattern should also override the description to describe the change. In #23 he clarified that statement to say that we don't want to get into the business of trying to build a regex parser to do that work automatically.
Thus, the solution to this issue is to have the Shortcut's machine name element override the description and also update the machine name Element's docs to add text saying "if you override the replacement pattern, then you also need to override the description."
All I know is that the IS says: "Shortcut machine names require hyphens." And I didn't look into why, but currently the element does replace disallowed characters with hyphens. I'm guessing there's no test coverage around it since the last change made by @prashant.c didn't break anything.
Comment #45
dcam commentedI updated the IS and issue title.
For the record, there's no restriction on using hyphens in machine names, other than what's defined in the element's replace_pattern. This is easy enough to test. Just make a Shortcut Set at
/admin/config/user-interface/shortcut/add-setthat contains spaces or other disallowed characters. The disallowed characters get replaced with hyphens with no problem. Save it and then view it in the config export page. There's no problem when you export the config to files either. It's saved with hyphens no problem.Comment #46
dcam commentedI reverted @prashant.c's changes because they were out-of-scope. Then I added more assertions that to verify that hyphenated machine names are allowed and valid, but underscores are not.
Comment #47
prashant.cThank you, @dcam, maybe I misunderstood the issue, so now hyphens are allowed whereas underscores are not.
Comment #48
smustgrave commentedI also misunderstood this ticket so apologize @prashant.c and @dcam.
So this is actually pretty straight forward and may not need the test coverage. Lets see what the committers think.
Comment #49
xjmI'd classify this as normal.
Comment #50
xjmMaking the title a bit stronger.
Removing credits for redundant manual testing.
Comment #51
xjmQueued fresh pipelines to run the test-only job, since I learned today it expires if the pipeline is more than a week old. (Contributors, you can do this from the "pipelines" local task on the MR page. The link for it is above the diff UI, not the one in the sidebar.)
Comment #52
xjmTest-only results:
That's good, but should we also be asserting that the shortcut set was not saved? Generally asserting error messages by itself isn't robust enough. So I just think we need to add one more assertion above this one about the entity being unchanged. Might have to clone it or whatnot.
There is also my above suggestion on the MR.
Thanks!
Comment #53
bramdriesenRemoved leading space from title.
Proofread all strings and added one extra comment to the MR.
Comment #54
dcam commented@xjm that assertion isn't testing for an error message. It's checking for the unique machine name element description. Since the focus of this issue is specifically about that description being incorrect I wrote the assertion to check for the corrected version. But since you asked, I also included assertions to check for whether a hyphenated machine name and another attempt to save an underscored machine were correctly saved. This was because a misunderstanding starting at #37 revealed that there was no test coverage for the hyphenated naming scheme.I addressed all feedback. Since the only changes were to code comments, one of which was written and suggested by a release manager, I'm going to be bad and self-RTBC this.
edit: sorry, I feel really stupid now. There is an assertion for the error message.
Comment #55
dcam commentedSorry, I'm clueless. I missed something important there.
Comment #56
dcam commentedOk, so I wasn't entirely stupid. The failing test that @xjm referenced is for the machine name element description. But there is a different assertion further down that checks for an error message. The description and error message have nearly identical text, resulting in the confusion we both had. @xjm was correct to request that an additional assertion be added. However...
There's no entity being changed here. It's an attempt to create a new shortcut set with underscores in the ID. It should be sufficient to check that an attempt to load the set returns NULL, which is what I added.
Comment #57
xjmRight, sorry, I was writing as if the shortcut set already existed, but what I should have said (what I was trying to say) is that no changes should be saved for the shortcut set since it was failing validation. And in the end that's what you tested. Sorry for misleading review comments and glad you were able to translate what I meant in the end!
Comment #58
smustgrave commentedHiding patches as fix is in MR.
Applied @xjm suggestion for a comment, also rebased the MR as it was 500+ commits back
Tests are still green
Test only ran
Feedback appears to be addressed on this one.
Comment #59
alexpottCommitted and pushed 34fa591813d to 11.x and febc710f53b to 11.2.x. Thanks!
Backported to 11.2.x because this is a bugfix.