2

I'm facing trouble coming up with the following approach:

I will have 3 separate forms acting on the same query from third party API's.

On the front page there is a shorter quick-form that would lead to the page of the other 2 forms. In some cases the information from the first form is sufficient to query the information and the information will be queried.

In other cases additional input from the second form is required to make the query. The third form is for fine-tuning an existing query.

The first form isn't an issue, it's a separate block on the front page that will store the information internally for the second form to see with multiform functionalities and on submit redirect to the second forms page, where the form can process the information and see if a query can be made or will give results and go ahead with it, if not, then ask for further input.

The second and third form are on the same page, where the second form has necessary input fields for the query, which can also be changed to change the results, and the third form is just for fine-tuning this query like a filter or so.

Right now I'm stuck with the second 2 forms working together on the same page, being separate forms but altering the same query/information. And would appreciate all and any help with this approach that would be "the Drupal way".

Better visualization:

Imagine a flight booking site: first form is your departure airport that takes you to the second 2 forms, where already a lot of flights from your airport can be displayed, second form takes in a destination, dates and passengers; the third form would let you fine-tune travel class, meals, airlines to avoid airlines preferred, how many layovers, how long layovers and so on..

All the information with each form is queried from different airline API's and will have to be redone on every change.

2 Answers 2

0

From your question... you are saying you have 3 forms...

The first form (on its own page) leads to the other 2 forms (which are on a different page, but both forms are on that single page)... it's not completely clear what the problem is - only that both forms are supposed to alter a query, but this is not happening?

Perhaps you could place the fields for both forms in a single form and assign different actions to each of the submit buttons... technically there will only be one form, but visually through styling you can make it appear as two separate forms through CSS - for example using tabs where the fields for the first form is on the first tab... and the fields for the second form is on the second tab... the first tab would have a submit button defined like this:

        $form['submit1']=array(
        '#type' => 'submit',
        '#value' => t('Submit form 1'),
        '#submit' => array('::processForm1'),
        );

and on the second tab you can define another submit button:

        $form['submit2']=array(
        '#type' => 'submit',
        '#value' => t('Submit form 2'),
        '#submit' => array('::processForm2'),
        );

Enough to say in the form object, you will need to define both the processForm1 function and the processForm2 function... in order to decipher which button triggered the submit in the formValidation and formSubmit functions you will use a line such as:

      $trigger=$form_state->getValue('op');

where $trigger will hold the label of the triggering button.

Hope that helps and makes sense.

0

You are loading a first form in a block. On click submit the first form. In first form submit handler redirect a page to result page i.e.. Create a new router in custom module routing yml file and controller method check all necessary information you got to show result, if yes then render the result array or else render second and third form.

While redirecting to result page pass $formstate object as first argument so that you can access the necessary argument and you also said filters are present in result page . If filters are ajax based then form ajax submit handler should load result render array. else pass filter values also in result page url.

You can render a form in a controller with code similar to the following one.

$result['content'] = \Drupal::formBuilder()->getForm('\Drupal\custom_module\Form\Formname', <arguments for the form>);

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.