Score:-2

Using Webform Submission, update a custom field attached to a user

za flag

EDIT: After working more on this, the user cannot be blocked and must be active / authenticated.

Using webforms, I have a form that has a field that a new user (blocked) needs to fill out to tell the admin what area they belong to. Is there a way that I can add the field value to the user (blocked, but currently filling out the form) account?

I have tried:

function helper_form_alter(&$form, FormStateInterface $form_state, $form_id)
{
  if (in_array($form_id, ['webform_submission_request_account_node_123_add_form'])) {
    $form['actions']['submit']['#submit'][] = '_helper_additional_insert';
  }
}
function _helper_additional_insert($form, &$form_state)
{
  ksm($form);
}

However, I can never see the ksm output, even having the form show modal or message for example. I tried outputting the value to the log also with no love.

I thought of using RULES, but I do not see how I can capture the field value and then update the current user field with it...

Suggestions? Thoughts? Thanks!

in flag
This is an [XY problem](https://meta.stackexchange.com/a/66378) and possibly describes a flawed implementation (why would you let an anonymous user edit a user entity?). Instead, describe what "blocked" exactly means, what is an "area", why the user is blocked, what this form is for, and why the user is filling it. Then we can talk about connecting the user to the form.
za flag
Right, so I had the idea that a blocked user has an available UID, but that is wrong. So I must allow the account to be active (authenticated without any permissions) in order for this to work. @Joseph thanks for adding a comment vs just a downvote also. So much more helpful in understanding why the downvote.
Score:0
za flag

Here is a working solution as long as the user is "active" and authenticated without any permissions that works for my usecase:

/**
 * Implements hook_form_alter().
 * This function is called whenever a form is altered.
 * Here, we are targeting the webform_submission_request_account_node_100_add_form form.
 */
function submission_helper_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (in_array($form_id, ['webform_submission_request_account_node_100_add_form'])) {
    // Add a custom submit handler to the form.
    // When the form is submitted, submission_helper_set_user_field() will be called.
    $form['actions']['submit']['#submit'][] = 'submission_helper_set_user_field';
  }
}

/**
 * Custom submission handler to set user field.
 * This function is called when the webform_submission_request_account_node_100_add_form is submitted.
 */
function submission_helper_set_user_field(&$form, &$form_state) {
  // Load the current user object.
  $user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
  
  // Get the value of the field_name field from the form.
  $selected_field_tid = $form_state->getValue('field_name');

  // Set the value of the field_user_field_machine_name field on the user object.
  $user->set('field_user_field_machine_name', $selected_field_tid);

  // Save the user object to the database.
  $user->save();

  // Load the taxonomy term object for the selected field.
  $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->load($selected_field_tid);
  
  // If the taxonomy term object exists, log the term name to the Drupal logger.
  if ($term) {
    $term_name = $term->getName();
    \Drupal::logger('submission_helper')->notice('User account requested and user field set to: @field.', ['@field' => $term_name]);
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.