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 |
|---|---|---|---|
| #9 | image (1).png | 3.87 KB | girish_lnweb |
| #9 | image (2).png | 5.3 KB | girish_lnweb |
| #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.
Comment #9
girish_lnweb commentedTested MR !15327 locally.
Environment: Drupal 11.4 (recommended-project, DDEV), PHP 8.3.30, PHPUnit 11.5.57. The MR diff applied cleanly with patch (one 5-line offset in UserData.php). I did not test on main.
drush test: with the fix reversed and the MR’s UserDataTest in place, testDataField fails with TypeError: Drupal\Component\Utility\Html::escape(): Argument #1 ($text) must be of type string, array given. With the MR applied, it passes (1 test, 6 assertions).
Manual test: I created a View on users with a “User: Data” field (User module, key test.key, caching set to None). Results with the MR applied (screenshots attached):
[10 => 'abc'] shows abc
['first','second','third'] shows first, second, third
['a' => ['b' => 'c']] shows a: {"b":"c"}
an empty object shows {}
an empty array shows an empty cell
plain text shows unchanged
No errors on any page.
AI disclosure: I used Claude to help plan the local test setup and steps, interpret the test output, read through the MR diff. I ran all commands and tests myself, observed the results shown above, and reviewed the text before posting.
Comment #10
smustgrave commentedStill appears to need summary update.
@girish_lnweb not sure if using AI is credit worthy, always better to do it yourself.