Can anyone help me on the below functions. I am trying to alter user registration form and add my user form fields to default registration form. I am successfully retrieving data till validation, but I am not sure where I am going wrong in hook_user_insert. I am not able to retrieve the field value of my custom fields in hook_user_insert function.

Early support will be helpful in completion of the module.

function security_questions_form_user_register_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  // Hide the questions form when the user is being registered by an admin.
  if (!\Drupal::currentUser()->hasPermission('administer users')) {
    // Merge in our user form.
    $form += SecurityQuestionsUserForm::buildForm($form, $form_state);
    array_unshift($form['#validate'], 'security_questions_form_user_register_form_validate');
    // We can't add our submit handler here, because the user account doesn't
    // exist yet. Instead, we will get the info during hook_user_insert().

 * Validation handler for security_questions_form_user_register_form_alter().
function security_questions_form_user_register_form_validate(array &$form, FormStateInterface $form_state) {
  //module_load_include('inc', 'security_questions', 'security_questions.pages');
  SecurityQuestionsUserForm::validateForm($form, $form_state);

 * Implements hook_ENTITY_TYPE_insert().
function security_questions_user_insert(\Drupal\user\UserInterface $account) {
  // During registration, save the user's answers.
  if (!empty($account->questions)) {
    security_questions_user_answers_save($account, $account->questions);
  • Remove the die; from your code.
    – leymannx
    Sep 25, 2019 at 16:08
  • ok I will remove it. Sep 26, 2019 at 10:35
  • 1
    In Drupal 8 the user entity is fieldable, so you could simply add a field to the user entity and it will be saved automatically. If you don't want that and handle the field value in custom code then try an #entity_builders callback, which you can add in the form alter hook, to use it instead of hook_user_insert(). See drupal.stackexchange.com/questions/163821/…
    – 4uk4
    Sep 26, 2019 at 11:36
  • Is this a theme? Related: drupal.stackexchange.com/questions/262855/… Oct 15, 2020 at 20:16

1 Answer 1


You probably need to add a field to the user entity, which matches your form field key. Or, save the data in the session ..

  • I have added the form from another form by using alter, in D7 this is done using drupal_retrieve_form and passing form_state as reference. It works in D7 with hook_user_insert. For D8 even if I use entity it fails to get that particular field values from the form. How to do that? Sep 26, 2019 at 10:35
  • If you cannot map the form fields to the entity fields, do it in form validate, by using $form['#entity'] to access the user entity.
    – rémy
    Sep 26, 2019 at 11:12
  • Can you give me an example? Sep 26, 2019 at 12:33
  • Can I use the custom submit handler instead of hook_user_insert? I tried that is also not working. If you can rewrite my code form alter and send me it would be helpful. Sep 26, 2019 at 12:47

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.