1

I am very new to drupal and also very willing to learn. I am trying to migrate old code from another tool to drupal 8. I am working on a form, using the webform module. Can I possibly show certain webform elements based on specific URL query strings? Example: test.com?user=employee, then show TextField = employee ID test.com?user=customer, then show Textfield = Company name.

Any help would be greatly appreciated.

1 Answer 1

2

You can create a hidden element called 'user' which can be prepopulated via the URL, then this hidden element can be used to conditionally hide/show other elements and even the submit button.

Below is the YAML source for a proof-of-concept.

user:
  '#type': hidden
  '#title': user
  '#prepopulate': true
customer_name:
  '#type': textfield
  '#title': 'Customer name'
  '#states':
    visible:
      ':input[name="user"]':
        value: customer
    required:
      ':input[name="user"]':
        value: customer
employee_name:
  '#type': textfield
  '#title': 'Employee name'
  '#states':
    visible:
      ':input[name="user"]':
        value: employee
    required:
      ':input[name="user"]':
        value: employee
actions:
  '#type': webform_actions
  '#title': 'Submit button(s)'
  '#states':
    visible:
      - ':input[name="user"]':
          value: customer
      - or
      - ':input[name="user"]':
          value: employee

Personally, I would change the 'user' from a hidden element to a select menu, just-in-case someone navigates to the form without any query string parameters, they would still be able to select the 'type of user'.

2
  • jrockowitz - rep! Loved your work on webform module and love the yt videos too. Will give this a go. Thanks!
    – mochalova
    May 3, 2019 at 13:25
  • By using a select menu, does that mean the user will first select what kind of user he is before the form loads? Not sure how that will fly if the user comes in without a query string?
    – mochalova
    May 8, 2019 at 13:57

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.