Problem/Motivation

Non-scalar values can be stored using the user.data service. The user module's views field UserData plugin passes whatever it gets back for the supplied key on to sanitizeValue() as if it was a string. This produces errors - for instance, when the user.data value is an array, the error is:

TypeError: htmlspecialchars(): Argument #1 ($string) must be of type string, array given in htmlspecialchars() (line 440 of /var/www/html/web/core/lib/Drupal/Component/Utility/Html.php).

Steps to reproduce

  1. Create a test module that stores an array in a user's UserData, like:
    \Drupal::service('user.data')->set('my_module', 1, 'test.key', [10 => 'abc']);
    
  2. Create a test view on user entities showing fields and add a 'User: Data' field, specifying your test module name and key value ("my_module" and "test.key" in this example)
  3. Try to display the view results for the user with the updated UserData

Proposed resolution

I don't know what would be considered appropriate, just returning the variable type ("Array"), a print_r($data, true), or doing something more elaborate.

Issue fork drupal-3572437

Command icon 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

brad.bulger created an issue. See original summary.

adhershmnair made their first commit to this issue’s fork.

adhershmnair’s picture

Status: Active » Needs review

Added a check for non-scalar values in UserData::render() before passing to sanitizeValue(). Non-scalar values (arrays, objects) are converted to a JSON string using json_encode() for readable display.

Reproduction confirmed:

  1. Store array in user data: \Drupal::service('user.data')->set('my_module', 1, 'test.key', [10 => 'abc']);
  2. Create a View with "User: Data" field for that module/key
  3. Before fix: TypeError in htmlspecialchars() because array is passed as string
  4. After fix: Displays JSON representation of the data structure
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs issue summary update, +Needs tests

Thank you for picking up

@adhershmnair have ot ask are you using AI? The post in #4 reads like it and you seem to have put a large number of issues in very very short time.

starlight-sparkle’s picture

There seems to already be similar handling for non-scalar values in \Drupal\views\Plugin\views\field\Serialized so perhaps UserData views plugin could extend from Serialized views plugin.

danielveza made their first commit to this issue’s fork.

danielveza’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new3.73 KB
new2.99 KB

Added test coverage and cleaned up the output a bit to be more user friendly that JSON. Added some screenshots that show what ['first', 'second', 'third'], and ['value' => 'test'] looks like.

Screenshot showing output from the userData plugin

Screenshot of output from the userData plugin.

Disclaimer that Claude helped with the views plugin changes, but I've reviewed and made changes were needed.