Is it possible to randomly select a fixed number of questions to display in a Webform? I have a request to set up over 500 questions in a webform but for each user who visits the webform, only a random selection of 100 questions will be displayed. The rest will not be displayed. The purpose of this is to use the Webform as an exam/quiz.

Note: the hidden questions cannot be visible in the code, so CSS or other Javascript hiding techniques are not suitable.

Any ideas how I might achieve it.

Thanks a mil



You can use form alter to access $form array and display/hide specific elements, more details: https://www.drupal.org/docs/8/modules/webform/webform-cookbook/how-to-programmatically-hide-and-show-webform-elements.

function CUSTOM_MODULE_form_webform_submission_WEBFORM_NAME_add_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  $elements = &Drupal\webform\Utility\WebformFormHelper::flattenElements($form['elements']);

  $elements['question_one']['#access'] = FALSE;
  $elements['question_two']['#access'] = FALSE;

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.