1

Is there any built-in way to display the questions on a webform and the user's responses as read-only?

The site I'm building allows a user to answer survey questions up to a given cut-off time. Once they've submitted the answers and the time-limit ends they must be able to see what they said, but not change it.

I can't see any obvious way of doing that, is there an easy answer? :-)

I know I can modify each field individually in a form_alter hook but that's not ideal...

1 Answer 1

0

I have done an identical task, unfortunately this one of those unique use cases. Here is a snipped in my hook_form_alter

Basically, assuming there is no page element (not multi page):

if ($cutoff_time > $current_time) {

  $form['elements']['field_name']['#disabled'] = TRUE;

  $messenger = \Drupal::messenger();
  $messenger->addMessage(t('Time has expired, you can no longer change the value'), $messenger::TYPE_WARNING);

}

Otherwise (multipage):

$form['elements']['page_name']['field_name']['#disabled'] = TRUE;
1
  • The long way... mmm. Not great. But thanks. Trouble is the code will have no idea how the webform is structured, and there could be some really long sets of questions, as well as custom element types. Maybe I should go for a chunk of JS instead, might be easier. (Though I don't like that idea much either.) Jan 8, 2019 at 11:58

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.