4

Background : #2833770 : Save user configs to states

Auto-logout saves the per-user settings using configuration API, which would obviously break with environment change and config deployment.

The suggested patch moves the storage from the config API to state API. My take on this and from the documentation of State API:

  1. State API is a better alternative which will avoid the deployment issues, but isn't perfect as the state can anytime be reset.
  2. The per-content configuration would get lost once that happens.

How often can a state API be reset? I would prefer some examples other than docs.

As an alternative, I think it would be safe to save the settings in a field on the user entity, but I would like to go this route only if state API is not a good option. I would really appreciate any input on the same.

4

Core provides a storage/API to store per-user information: the user.data service. It also provides a very simple example implementation in the Contact module: See contact_form_user_form_alter() and contact_user_profile_form_submit().

\Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact'));
$account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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