0

In Drupal 7, there is user_save($account, $edit = array(), $category = 'account'). How do I create a new category for an existing account in Drupal 8?

Also, $user = \Drupal::currentUser(); and $user->save(); gives error as well. I need to attach a form element value to user object.

How to link a login form element to user account on logging in?

1

The category concept has been removed. So the answer is, you don't.

Not sure what you are trying to do, if you need a better answer, you will need to provide more details.

User is an entity with fields. There's nothing special about it, you change and save data in the same way like any other entity.

  • In the login form i have added a checkbox(say xyz). Based on the value of checkbox i need to attach a new field/value which stores the value of this checkbox in user object. I need save this value in user object on login form submit. (May be using hook_user_login, hook_user_presave or hook_form_alter). in drupal -7 it happens as user_save($account, $edit, 'xyz'), where $edit['input']['xyz'] = 1. – neha Jul 3 '16 at 16:11
  • Basically i have to attach the form element to user account. – neha Jul 3 '16 at 17:12
0

You cannot create a new category, since in Drupal 8 the category system has been removed. As said in The category system is removed from user edit and view, you just need to remove any reference to user categories in your code.

If you need to add a custom form field to the user edit form, and save the submitted value, you simply:

  • Implement hook_form_alter() to add the new form field, and a form submission handler
  • In the form submission handler, save the value submitted for the form field you added

hook_form_alter() and hook_form_FORM_ID_alter() still works for Drupal 8. You just need to know correct form ID for the user edit form, which is user_form. See for example contact_form_user_form_alter(), which shows also a way to save custom data associated with user accounts.

  $form['contact'] = array(
    '#type' => 'details',
    '#title' => t('Contact settings'),
    '#open' => TRUE,
    '#weight' => 5,
  );
  $account = $form_state->getFormObject()->getEntity();
  if (!\Drupal::currentUser()->isAnonymous() && $account->id()) {
    $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
  }
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
    '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
    '#description' => t('Allow other users to contact you via a personal contact form which keeps your email address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.'),
  );
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';

As you see, the code uses the user.data service to retrieve the value submitted for the checkbox it adds. contact_user_profile_form_submit() then shows how to save the value submitted for that checkbox.

  $account = $form_state->getFormObject()->getEntity();
  if ($account->id() && $form_state->hasValue('contact')) {
    \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state->getValue('contact'));
  }

As for why $user = \Drupal::currentUser(); $user->save(); doesn't work, that is simply because the object returned from \Drupal::currentUser() is an object implementing AccountProxyInterface, which doesn't implement any save() method. The only way to alter the user account for the logged-in user is using code similar to the following one.

use \Drupal\Core\Session\AccountProxyInterface;
use \Drupal\user\Entity\User;

$user = User::load(\Drupal::currentUser()->id());
if ($user) {
  // Add the administrator role.
  $user->addRole('administrator');
  $user->save();
}
  • I do not want to use profile module just for this feature. I want do to something like $user->set("new_element", 1); $user->save() inside submit handler. – neha Jul 4 '16 at 11:53
  • In that case, you should not even use the category system in Drupal 7. – kiamlaluno Jul 4 '16 at 11:57
  • Let's leave this in d-7. How to do in D-8? – neha Jul 4 '16 at 11:59
0

Here is piece of my working code:

To attach the element & set the value:

\Drupal::service('user.data')->set('module_name', $account->id(), 'category_name', $category_value);

To fetch the value:

$account = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  $remember_me_value = \Drupal::service('user.data')->get('module_name', $account->id(), 'category_name');

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.