1

I'm using Drupal 8 and I wonder what the purpose is of the user_data table? How can I can store custom data in that table? In my site, the content of that table is the following.

screenshot

1 Answer 1

6

This table is the backend for the User Data service. The User Data service is used to store data specific to users in the system.

You can set data like this:

\Drupal::service('user.data')->set($module, $uid, $key, $value);

Where:

$module = the machine key of your module

$uid = the UID of the user for whom data should be retrieved

$key = a key to identify which data you are storing

$value = the value to be stored

This data can be retrieved with the get() method:

$value = \Drupal::service('user.data')->get($module, $uid, $key);

$value will be equal to the $value you used when setting the data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.