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
- 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']); - 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)
- 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.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | Screenshot from 2026-08-31 08-31-39.png | 2.99 KB | danielveza |
| #8 | Screenshot from 2026-08-31 08-31-24.png | 3.73 KB | danielveza |
Issue fork drupal-3572437
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 #4
adhershmnair commentedAdded a check for non-scalar values in
UserData::render()before passing tosanitizeValue().Non-scalar values (arrays, objects) are converted to a JSON string using json_encode() for readable display.Reproduction confirmed:
\Drupal::service('user.data')->set('my_module', 1, 'test.key', [10 => 'abc']);Comment #5
smustgrave commentedThank 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.
Comment #6
starlight-sparkleThere seems to already be similar handling for non-scalar values in
\Drupal\views\Plugin\views\field\Serializedso perhapsUserDataviews plugin could extend fromSerializedviews plugin.Comment #8
danielvezaAdded 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.Disclaimer that Claude helped with the views plugin changes, but I've reviewed and made changes were needed.