0

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

A sample of what I want to achieve:

some_question:
  '#type': tableselect
  '#title': 'some question'
  '#options':
    q1: 'q1 (hide if q2 is checked)'
    q2: 'q2 (hide if q1 is checked)'
    q3: q3
    q4: q4
    q5: q5
    q6: q6
    q7: q7

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
0

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
    kint($form); 
    // 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!

| improve this answer | |
  • 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
  • sorry for being months late. I managed to create the custom module and made sure it's working but the function in your answer unfortunately isn't working. The form_id I'm using is correct afaik (I tested with an echo call and it's attaching) and the checkboxes names correct too. Any ideas ? – Adam888 Feb 15 at 7:06
  • If your $form_id is correct, then I guess the issue comes from the #states code. Make sure that you are targeting the right element from the $form array (for instance, I have webforms where my fields are stored in an "elements" array). So my code would end up with $form['elements']['my_checkbox_name']['#states'] = array(...); Sorry if I can't help you more but it's difficult for me without knowing the structure of your form and having a look at your hook_form_alter(). – misterdidi Feb 18 at 8:51
  • Thanks a lot @misterdidi . I'd actually given up the whole hook_form_alter way and re did the questions involved as separate elements so I could set their states via the built-in conditions manager. I still love to figure this out and find the culprit. I edited the question and added at the end a sample to illustrate the type of element and what I want to achieve if that may help. Otherwise, I appreciate all the help and I'm going to accept your answer since it indeed answers the essence of my question and helped me with my first custom module. – Adam888 Feb 19 at 1:54
  • @Adam888: If I understand the sample you added, you actually wanted to alter the options of an element depending on its own options values, right? As far as I know it is not something you can handle with #states (#states listens to the other form elements, but cannot listen on the very element it is set). Instead, I would use custom Javascript to achieve what you're looking for. – misterdidi Feb 24 at 14:26

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.