Improving the verification of the user field in the AccessStorage service

Created on 3 June 2024, 4 months ago

Problem/Motivation

First of all, thank you very much for your module.

In the case of my application, I have to disable the 'user' field in a hook_form_taxonomy_term_form_alter(), because I want to limit the choice only to roles.

function HOOK_form_taxonomy_term_form_alter(&$form, FormStateInterface $formState, $formId): void {
  unset($form['access']['user']);
}

However, when I edit a term and validate, I get an warning message.

Warning: Undefined array key "access" in Drupal\permissions_by_term\Service\AccessStorage->getSubmittedUserIds() (line 371 of modules/contrib/permissions_by_term/src/Service/AccessStorage.php).

I have tried many ways :

* with unset($form['access']['user']);
* with $form['access']['user']['#access'] = FALSE;
* with $form['access']['user']['#attributes']['disabled'] = TRUE;

But whatever method I choose, I still get this error message because the field is disabled and Drupal doesn't send it in the request.

Steps to reproduce

To reproduce this problem easily, simply :

* Configure a vocabulary so that permission can be applied to it.
* Implement in a hook_form_taxonomy_term_form_alter().
* Deactivate the user field, as shown in the description of the bug
* Modify a term and submit.

Proposed resolution

The method getSubmittedUserIds() in the AccessStorage service handles the Drupal response in a very strange way, as documented, but I don't know enough about the module to understand why. Nevertheless, this problem can be corrected very simply, in one line, by making sure that the 'user' entry exists in the request table.

# \Drupal\permissions_by_term\Service\AccessStorage::getSubmittedUserIds()
$sRawUsers = $_REQUEST['access']['user'] ?? [];

I'm well aware that the case of my application is specific. Nevertheless, this refinement makes the module code more robust and may prevent other errors in similar circumstances.

Remaining tasks

Nothing, I'm proposing an MR.

🐛 Bug report
Status

Active

Version

3.1

Component

Code

Created by

🇫🇷France zewebmaster Nantes

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024