How to Set Hidden Form Field Values Permanently via Form Alter I have a webform that gets some hidden form fields set based on the referring URL.  Within hook_form_alter, I can successfully set the hidden values with $form[elements][NAME]['#value'].

function MODULE_form_alter(&amp;$form,\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

 //Get referring Program path/URL
 $previousUrl = \Drupal::request()-&gt;server-&gt;get('HTTP_REFERER');

 if ($form_id == FORM_ID) {

    $prog = getLeadProgramContact($previousUrl); 
    $form['elements']['referrer']['#value'] = $prog-&gt;field_email-&gt;value;
    $form['elements']['program_name']['#value'] = $prog-&gt;getName();
     }
}


However, when the form gets submitted, it appears hook_form_alter is called again, breaking everything, since the referring URL is now itself. 

How can I permanently set hidden field values on initial form render?  Anything I try with hook_form_alter seems to fail.  I have also tried "#default_value"
Ok webforms are a bit tricky ... but this will work i don't really care to set hidden fields just set the data direct to the entity.  

if this is not a multy step form you could probably ad another submit handler in your form alter aka ...

function MODULE_form_alter(&amp;$form,\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
   if ($form_id === FORM_ID) { 
     $submit[] = 'MODULE_form_alter_submit';
  }
}
// then 
function MODULE_form_alter_submit(&amp;$form,\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
     // get the data you need.. via this ..
    $previousUrl = \Drupal::request()-&gt;server-&gt;get('HTTP_REFERER');
    $prog = getLeadProgramContact($previousUrl); 
    // or ($_GET) or session or cookie whatever you chose.. Then set    it 

    // GET the form Object.
    $webform_submission_object = $form_state-&gt;getFormObject();
    // GET the submission object.
    $webform_submission = $webform_submission_object-&gt;getEntity();
    // Optioanl default values if need to play with them .. 
    $orig_data = $webform_submission-&gt;getOriginalData();
    // set form state
    $form_state-&gt;setValue('referrer', $prog-&gt;...);
    // set submission field value.  
    $webform_submission-&gt;setElementData('referrer', $value);

    $form_state-&gt;setValue('program_name', $prog-&gt;...);
    $webform_submission-&gt;setElementData('program_name', $value);
    // Save it so it cant go away.
    $webform_submission-&gt;save();

  }
 }
}