0

Here's a little background on my project: I have 10 or so webforms let's call them Forms A thru J which are for different departments of a college. I also, have 1 credit card processing form let's call Form Z. I will be collecting information from a form like Form A specific to that departments needs and upon submission will redirect the user to Form Z to process a credit card transaction. I have created a webform handler to redirect the user from Forms A-J to Form Z and a handler to process the credit card transaction. In the redirect handler I am able to pass all of the information from Forms A-J to Form Z using the query string in the URL. I want to avoid this since there would be Personal Identifiable Information (PII) being passed through the URL.

I want to be able to retrieve the data submitted on say Form A from the database when Form Z is loading. I should be able to pass the webform node ID and submission ID to Form Z through the URL query string and use them to get the data from the database. I found these posts which give me an idea of how to retrieve the data from the database:

Programmatically update Webform Submission by sid

Load webform submission

https://www.drupal.org/project/webform/issues/2911356

What I can't find is how do I get this to run when Form Z is loading. I've seen a couple of posts on hook_form_alter but those posts are using it in a different way from what I am trying to do so I don't know it that's the way I need to go.

Once I retrieve the data from the database I need to populate that data into I'm thinking some hidden webform fields on Form Z. I'd like to be able to fill billing address with the address submitted with the previous form upon request (checkbox or button). Also, I need to be able to have the data from the previous form available once Form Z submits so that I can send a custom email Confirmation depending on which form the information came from (i.e. Form A or B or J).

Being new to drupal development I've gotten some great assistance with this project from this site and I am very grateful to those who have been assisting me. :)

EDIT:

So based on Remy's recommendations I have created a custom module with these two files in \modules\custom\cc_processing_getsubmissiondata:

cc_processing_getsubmissiondata.info.yml

name: 'CC Processing Get SubmissionData'
description: 'Provides a custom webform handler for retrieving the previous forms submission data'
core_version_requirement: ^8.8
Core: 8.x
package: Custom
type: module

CCProcessingGetSubmissionData.module

<?php

/**
 * Implementation of hook_form_alter
 */
 
 use Drupal\Core\Form\FormStateInterface;
 
 function CCProcessingGetSubmissionData_form_alter(&$form, FormStateInterface $form_state, $form_id) {
    echo "form alter hook triggered";
        $form['street_1'] = '92-2000 Some Rd.';
    
    if($form['#webform_id'] == 'credit_card_billing_information') {
        echo "form alter form id found";
        $form['credit_card_name'] = 'Jared';
    }
 }

At this stage I am just trying to get the hook_form_alter to just do something so that I know it is being triggered. However, I am getting nothing. I even tried the function name CCProcessingGetSubmissionData_webform_credit_card_billing_information_form_alter but that didn't work either. Any suggestions?

3
  • 1
    Try a multi-step (wizard) form ;) - drupal.org/docs/8/modules/webform/…
    – rémy
    Commented Mar 31, 2021 at 19:37
  • If at all possible I want to keep the process as object oriented as possible. Using multi-step (wizard) form, I'd have a credit card processing step on every form on Forms A-J. That number could grow in the future which just means if I needed to make any modifications to the credit card processing step, I'd have to do it 10+ times for each form.
    – iJared
    Commented Mar 31, 2021 at 21:59
  • There are nice yaml import and export features ;)
    – rémy
    Commented Mar 31, 2021 at 23:27

1 Answer 1

2

Easiest is probably to preprocess the webform's new submission, load the desired submission, and then pre-populate your fields. I doubt that this is built-in.

fe

<?php
function HOOK_webform_submission_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form['#webform_id'] == 'your_webfom') {
    $form['elements']['your_element']['#default_value'] = $your_value;
  }
}

There may be several ways by not-coding, but using modules.

LMGTFY - https://www.drupal.org/forum/support/post-installation/2021-02-01/how-to-use-webforms-submissions-in-other-forms

  1. Install the 'Webform Views Integration' module.
  2. Create the view for display submissions of the 'Client' webform.
  3. Add to the view the 'Entity Reference' display and add the 'Name' field (or any field you want to see on the 'Project' webform).
  4. Click on the 'Settings' and check the field that will use for serach.
  5. In the 'Client' webform select the following reference method: 'View: Filter by an entity reference view'.
  6. Select the view created by you from the 'View used to select the entities' list.

The screenshots of the example:

  1. https://ibb.co/ZJxgK9w
  2. https://ibb.co/C6sxCFv
  3. https://ibb.co/mFWDCTX
  4. https://ibb.co/QcwYYm6
2
  • So there's 2 reasons this won't work for us. First, this seems like it would populate a dropdown list on Form Z with the names or some field from lets say Form A. Then on Form Z the user would have to select their name in the dropdown list to populate the fields in Form Z. This won't work because that would mean anyone could select any name in the dropdown list thus exposing the PII I am trying to avoid exposing in the URL. I would be better off using the URL query string. Secondly, those who control our Drupal installation won't approve use of a module that hasn't already passed release.
    – iJared
    Commented Mar 31, 2021 at 22:17
  • As you seem to be sophisticated team, go for the pre-population approach - see my updates.
    – rémy
    Commented Mar 31, 2021 at 23:19

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.