In a custom submit handler, I want to set a field value for a text field (my_field_machinename
) in my webform. The handler is getting called correctly, but the value is not getting stored. What do I have to change in the code below to make it work?
I know I could use the presave hook, but I would prefer to make the alteration within the submit handler.
class CustomHandler extends WebformHandlerBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state, WebformSubmissionInterface $webform_submission) {
$this->submitMyFieldData($form_state);
}
/**
* Set my field values
*/
private function submitMyFieldData(FormStateInterface $formState) {
$formState->setValue('my_field_machinename', [['value' => '123']]); // Does not work
}
}
$this-> submitMyFieldData
fix this and retry. Then please update your question and a little bit of detail what exact field you are referring to. You want to hardcode a value in the webform submission? Or is this a node field? What are you trying to achieve and why?$form_state->setRebuild();
insubmitForm