I need to create #states conditions between either the fields in any one element in a webform or across other elements in the same form. I'd hoped I could do it via the WF yaml editor but it seems I couldn't [1]. As per @jrockowitz 's comment on the attached link #1, I

might be able to use a form alter hook to add the needed #states

I am not a developer but it seems with drupal 8+ I do need to learn to start to be one and that's on my list for sure. I'd love to be able to contribute something to Drupal and especially webform but I find the learning process intimidating especially with the lack of "beginner-type" tutorials at least when it comes to creating custom modules with more than "hello world!".

For now, I need to know the steps I need to follow @jrockowitz 's advice and use hook_form_alter to serve my goal. My understanding is that could be done either on a theme level or custom module level (please correct me if wrong) and since I'm more comfortable with the theme way and I feel it's simpler for someone like me, I like to know (at least):

  1. which files ( other than MYTHEME.theme) I need to edit or/and create to start with and
  2. what info do I need to look for and study so I can do the job and begin my learning journey.
  3. I learn best by example and so any extra clear a-b-c type tips to get me started is greatly valued if any.

If the custom module way is more advisable or the only way:

  1. What is the minimal skeleton of a custom module that I need to have for the purpose.
  2. What do I need to study to learn how to create such a module.

(I'm not sure if the last question should be a separate question so please forgive my newbie status here but I promise I've done my best)

Thanks so much everyone

Related links:

[1] Why isn't my custom Form API #states applied in my webform yaml editor working?

[2] https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21form.api.php/function/hook_form_alter/8.7.x

[3] https://www.jrockowitz.com/blog/add-ons

  • Before heading to custom dev, can't your goal be achieved directly from your Webform admin interface with webform field conditions? – misterdidi Jun 20 '19 at 8:16
  • @misterdidi thanks for your comment. I'd love to start learning custom dev for more than a reason but for now, unfortunately, it seems no I cannot do it via the UI like I stated in my post according to my other question which led me to this q. drupal.stackexchange.com/questions/282349/… – Adam888 Jun 20 '19 at 10:22

Ok, if you need to rely on custom dev, you can indeed use hook_form_alter().

It would work if you set it in your mytheme.theme file but for unrelated theme issues I prefer to rely on custom modules. To create your module:

  • Create a mymodule.info.yml file in the folder /modules/custom/mymodule of your Drupal website. See this page for providing your file with the minimal content to have your module detected by Drupal.
  • With only this file you should be able to enable your module
  • Create a mymodule.module file at the same level as your .info.yml file
  • In your .module file (or mytheme.theme if you chose not to create a module), add the following:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  // Target your form
  if($form_id == "your_form_id") {
    // Use Devel Kint module to display the architecture of your form and make it easier for you to find the elements you wish to alter
    // Now you can access the elements of your form and edit the desired ones

    // Hide checkbox "my_checkbox_name" if checkbox "my_other_checkbox" is checked
    $form['my_checkbox_name']['#states'] = array(
      'invisible' => array(
        ':input[name="my_other_checkbox"]' => array('checked' => TRUE),
    // Make field "some_input_text" required if input field "some_other_input" is filled with value 'hello'
    $form['some_input_text']['#states'] = array(
      'required' => array(
        ':input[name="some_other_input"]' => array('value' => 'hello'),

I recommend you to have a look at this page for the list of available #states and some examples as well.

Happy coding!

  • wow! just that ?! I thought more files would be needed. Thanks so so much for your kind help and clear answer. I can't wait to try it and update you with my results – Adam888 Jun 20 '19 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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