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
It is possible to create a role programmatically with invalid machine name characters:
$role = \Drupal\user\Entity\Role::create([
'id' => 'aBc !',
'label' => 'zyx',
]);
$role->save();
Then navigate to /admin/people/roles/manage/aBc !
You can no longer save the form because machine names are immutable.
Proposed resolution
? - Refer to parent #2845517: Enforce allowed characters of machine names in config entities.
Original report by suresh kumara
User role machine name created with invalid characters
How to replicate issue?
I have create role using drush.
drush role-create 'Unapproved Teacher'
The machine name of a role created as Unapproved Teacher.
The machine name of a role should be unapproved_teacher.
Comment | File | Size | Author |
---|---|---|---|
#4 | role_invalid_machinename.jpg | 104.78 KB | dpi |
Edit role ipassio.png | 65.98 KB | FlutterStack |
Comments
Comment #2
Anonymous (not verified) CreditAttribution: Anonymous commentedLooks like drush problem, because it create role via create Role instance directly. Proof:
But maybe we must override Role construct with the necessary checks, for stable work via third-party applications?
Comment #3
alexpott@suresh kumara thanks for reporting this issue. Before we can work out what to do here we need a bit more information. Why is this a critical issue? What are the effects on the site? I agree we have issues around configuration validation when creating config via the API and not the UI but an contributed project abusing the API does not make a critical. I'm going to tentatively just mark this a normal bug and relate it to the work on config validation.
Comment #4
dpiReformatted issue summary, linked to relevant parent.
Comment #6
joshi.rohit100I think a constraint (if already not available) will be good to check violation.
Comment #14
larowlan#2920678: Add config validation for the allowed characters of machine names