0

I am trying to alter the data in a webform submission before this is being saved in to the database.

In D7 i would do something like this:

function mymodule_webform_submission_presave($node, &$submission){
  if($submission->nid == 1234){ 
    $n = node_load(4321);
    $value = $n->field_value['und'][0]['value'];
    $submission->data[2][0] = $value;
  }
}

However I am not able to achieve the same in Drupal 9. I have tried using some of the hook_webform_handler_invoke_alter and hook_webform_handler_invoke_METHOD_NAME_alter hooks listed in webform.api.php without success. Are these the correct hooks to use, I have not been able to identify any "submission_presave" hooks in the Drupal 9 version of webform module (currently v 6.2.2). Any help or guidance toward the best solution would be appreciated.

2
  • webform_submission is an entity type, so you'd use hook_ENTITY_TYPE_presave - which gives you the same function name as your Drupal 7 version. You'll need to change the function signature (first arg will be $entity, the submission), and the method body to manipulate the new entity structure, but that's where you'd do it
    – Clive
    Jan 31 at 10:56
  • Clive, thank you for your answer, I appreciate it :) -- I have tried that hook as well, but I must be doing something wrong. I have tried a function called mymodule_webform_submission_presave(Drupal\webform\Entity\WebformSubmission $webform_submission){} and a lot of variations of that - but i can't seem to get them to work, or access/dump the submission data.
    – hasse88
    Jan 31 at 11:35

1 Answer 1

1

You can use hook_ENTITY_TYPE_presave() to alter webform submission data before insert:

function Mymodulename_webform_submission_presave(\Drupal\webform\Entity\WebformSubmission $webform_submission){
  // Get webform type/variation
  $webform_type = $webform_submission->getWebform()->id();
  if($webform_type == 'contact'){
    // Get user submitted data 
    $data = $webform_submission->getData();
    // Alter the data of title field
    $data['title'] = 'Testing tt';
    // Pass altered data 
    $webform_submission->setData($data);
  } 
} 

Note: can pass parameter that specific to hook Drupal\webform\Entity\WebformSubmission $webform_submission or in this way \Drupal\Core\Entity\EntityInterface $entity both are correct

You can follow this refrence to read more about hook_ENTITY_TYPE_presave() hook.

6
  • I disagree, I think it's better to use the actual type that's expected, rather than the generic one, for $entity. It's simply more accurate. It definitely isn't "incorrect" to do it that way, as your answers implies
    – Clive
    Jan 31 at 17:50
  • Thanks for flagging it out , however I did not delete the ans as it's generic code to update webform submission programatically
    – mini
    Jan 31 at 18:38
  • Thanks very much, that seems to work. And in addition - if I want to dump the data, but not have to resubmit the form every time, would that be possible. If i dpm($webform_type), it dumps the webform id on the confirmation page, but if I then afterwards want to check what the $data array returns, I have to go back and fill out the form again and dpm($data). Would be nice if I could just refresh the page to give me the next dpm(), and throwing in an exit(); after the dpm($webform_type); just gives me a completely white screen. I tried var_dump(), print_r(), dump() as well with the same result.
    – hasse88
    Feb 1 at 6:53
  • If ans works fine , can you please vote up so that it gets its recognition
    – mini
    Feb 1 at 9:10
  • You can use drupal logger function like this \Drupal::logger('module_name')->notice('<pre><code>' . print_r($webform_submission->getData()), TRUE) . '</code></pre>' ); or use this function dd($webform_submission->getData()) to print webform submission data
    – mini
    Feb 1 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.