0

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.
  print_r($account->questions);
  die;
  if (!empty($account->questions)) {
    security_questions_user_answers_save($account, $account->questions);
  }
}
4
  • Remove the die; from your code. – leymannx Sep 25 '19 at 16:08
  • ok I will remove it. – Thamizhvanan Sep 26 '19 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/… – 4k4 Sep 26 '19 at 11:36
  • Is this a theme? Related: drupal.stackexchange.com/questions/262855/… – Chris Happy Oct 15 '20 at 20:16
0

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

4
  • 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? – Thamizhvanan Sep 26 '19 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 '19 at 11:12
  • Can you give me an example? – Thamizhvanan Sep 26 '19 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. – Thamizhvanan Sep 26 '19 at 12:47

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.