3

I've seen that it was possible to store custom meta data in Drupal 7 but I didn't find the equivalent for Drupal 8 ?

https://martsie.github.io/2015/03/04/drupal-how-to-use-user-data/

The data that I need to store is temporary user data that might differ from each users and it's not needed to put it as field (no sql query needed).

1 Answer 1

11

You can use the user.data service

\Drupal::service('user.data')->set('mymodule', $uid, 'some_key', 'some_value');

You can then retrieve it with the same service:

$some_key = \Drupal::service('user.data')->get('mymodule', $uid, 'some_key');
4
  • 3
    The relevant changelog page - drupal.org/node/1852360
    – zerolab
    Commented Jan 31, 2017 at 12:17
  • Does this get deleted by itself? or how would you delete it?
    – No Sssweat
    Commented Jan 31, 2017 at 13:33
  • 4
    It gets deleted automatically when a user gets deleted or when a module is uninstalled.
    – Berdir
    Commented Jan 31, 2017 at 16:09
  • I didn't know that about the module un-installation. That's good information!
    – Jaypan
    Commented Feb 1, 2017 at 2:14

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.