2

I created my webform to accept parameters via a standard $_GET and this works fine but is quite unsecure I am told and should rather use clean URL's via Drupal 8 built in Routing.

As such I set up a module to support my webform and contains only a info.yml and routing.ynl. The routing is as below, I got the format from Using parameters in routes but suspect I just haven't grasped it.

abc_enquire_form_routing.webform:
  path: /form/enquire/{type}/{region}/{destination}
  requirements:
    _permission: 'access content'
  defaults:
    type: 'tailor-made-holidays'

Normal form access is via http://abcsite.localhost/form/enquire

Form access with parameters as a get (For illustration) was: http://abcsite.localhost/form/enquire?type=tailor-made-holidays&region=asia&destination=48

Module name is: abc_enquire_form_routing

Trying to access the form with the module enabled with the above routing through http://abcsite.localhost/form/enquire/tailor-made-holidays is simply giving me a p "The requested page could not be found." error. So I can only assume I am doing the routing wrong.

What I am trying to achieve is three optional parameters after the "http://abcsite.localhost/form/enquire" page of the url, e.g. http://abcsite.localhost/form/enquire/tailor-made-holidays/asia/49

Can anyone point me in the direction of a good example of doing this with webforms module ?

2
  • Routes can't support query string parameters. You might need to define a route for each variant. abc_enquire_form_routing.webform --abc_enquire_form_routing.webform.type -- abc_enquire_form_routing.webform.type.region
    – jrockowitz
    Commented Nov 6, 2017 at 20:47
  • @jrockowitz - Thanks, I was trying to actually do away with Querystring params, and switch to a purely RESTful method of passing parameters within the route, but I cannot get the route working if I add parameters as shown above.
    – John Cogan
    Commented Nov 8, 2017 at 9:12

1 Answer 1

0

I found really weird the response from jrockowitz. Because what you are asking is indeed possible.

If someone else needs a proper response for this question, then you can easily do this:

abc_enquire_form_routing.webform:
  path: /form/enquire/{type?}/{region?}/{destination?}
  requirements:
    _permission: 'access content'
  defaults:
    type: 'tailor-made-holidays'

That way if don't add the last three parameters you won't receive any error at all.

And of course you can use them as you want in your controller :)

Btw, you can also remove the defaults key if you don't need it:

 abc_enquire_form_routing.webform:
  path: /form/enquire/{type?}/{region?}/{destination?}
  requirements:
    _permission: 'access content'

Remember to clear your cache after this implementation.

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.