0

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(&$form,\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

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

 if ($form_id == FORM_ID) {

    $prog = getLeadProgramContact($previousUrl); 
    $form['elements']['referrer']['#value'] = $prog->field_email->value;
    $form['elements']['program_name']['#value'] = $prog->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"

  • could you simply use a $_GET var ? is this a webform submission (as looks like it) ... if not have you thought of using "hook_entity_insert" ? – Taggart Jensen Jun 13 '18 at 23:53
0

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(&$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(&$form,\Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
     // get the data you need.. via this ..
    $previousUrl = \Drupal::request()->server->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->getFormObject();
    // GET the submission object.
    $webform_submission = $webform_submission_object->getEntity();
    // Optioanl default values if need to play with them .. 
    $orig_data = $webform_submission->getOriginalData();
    // set form state
    $form_state->setValue('referrer', $prog->...);
    // set submission field value.  
    $webform_submission->setElementData('referrer', $value);

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

  }
 }
}
  • T: Tried this; still same deal, although it is updating the entity successfully. All the docs I have read say that hook_form_alter gets called "before a form is rendered." With Webform, it appears hook_form_alter gets called when submit is clicked, too. – Peter T Graber Jun 19 '18 at 20:03
  • I refactored and is working now. Thanks, Taggart, for the insight! – Peter T Graber Jun 20 '18 at 15:08

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.