Configured actions created by User module cannot be edited

Created on 9 June 2020, about 4 years ago
Updated 22 April 2024, 2 months ago

Problem/Motivation

The User module has some code that creates configured actions to add/remove a role from a user, when a new role is created.

The machine name (configuration entity ID) chosen for these actions is incompatible with the edit form in the Actions module UI.

So if you attempt to edit one of those auto-created actions, you get an error message saying:

 The machine-readable name must contain only lowercase letters, numbers, and underscores. 

To reproduce:
- Turn on Actions module
- Go to admin/config/system/actions
- Click Edit for one of the "Add ... role to " actions
- Click Save and you will see the message
- It looks like from that screen that you should be able to edit the machine name, but this is actually not possible as the machine name field is read-only (it is configuration and you cannot change its ID once it is created).

Proposed resolution

Fix the User module so that the actions it creates have legal machine names. The code that creates the actions is in user.module:

function user_user_role_insert(RoleInterface $role) {
...
  $add_id = 'user_add_role_action.' . $role->id();
  if (!Action::load($add_id)) {
    $action = Action::create([
      'id' => $add_id,
 ...
  }
  $remove_id = 'user_remove_role_action.' . $role->id();
  if (!Action::load($remove_id)) {
    $action = Action::create([
      'id' => $remove_id,
  ...

OR

Fix the Actions module so that it allows . in the machine name in the edit form. The code for that is in
core/modules/action/src/Form/ActionFormBase.php:

    $form['id'] = [
      '#type' => 'machine_name',
      '#default_value' => $this->entity->id(),
      '#disabled' => !$this->entity->isNew(),
      '#maxlength' => 64,
      '#description' => $this->t('A unique name for this action. It must only contain lowercase letters, numbers and underscores.'),
      '#machine_name' => [
        'exists' => [$this, 'exists'],
      ],
    ];

Allowing . can be specified by changing the default for the #machine_name['replace_pattern'] property. It defaults to '[^a-z0-9_]+'. If this is changed the #description also needs to be changed.

I think this is the best option since config will most likely already contain uneditable actions.

Remaining tasks

Decide which option makes the most sense. Make a patch.

User interface changes

All configured actions will be editable.

API changes

None.

Data model changes

None.

Release notes snippet

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component
User module 

Last updated about 2 hours ago

Created by

🇺🇸United States jhodgdon Spokane, WA, USA

Live updates comments and jobs are added and updated live.
  • Usability

    Makes Drupal easier to use. Preferred over UX, D7UX, etc.

  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024