I have a webform that is very simple, it's a questionnaire. It is attached to a content type called "event". e.g.

  '#type': likert
  '#title': 'Discussion 1'
    'Question 1': 'Question 1'
    'Question 2': 'Question 2'
    'Question 3': 'Question 3'
  '#answers': likert_five_scale

Is it possible to alter this webform when the webform is rendered by populating the questions from another source such as a view?

i.e. there will be a variable number of discussions with variable numbers of questions.

I am trying to dynamically build a webform based on data that is coming from the node to which the webform is attached


You need to use hook_webform_load() and alter a webform's elements via \Drupal\webform\Entity\Webform::setElements. If you trying the alter webform elements based on the node (aka source entity) you might need to use hook_webform_submission_load() and then alter the webform's element via \Drupal\webform\Entity\WebformSubmission::getWebform

  • Thanks Jacob, as usual you live up to your name - You Rock! How do you get so much time to help people out? – Tim C Jun 24 '19 at 13:31

Create a custom module and use hook_form_alter to add new fields and set default values.

function YOURMODULENAME_form_alter($form_id, &$form) {
  if ($form_id == 'YOUR_FORM_ID') { 
        $form['yourmodule'] = array(
        '#type' => 'checkbox',
        '#options' => t('Test'),
        '#title' => t('Title),
        '#description' => t('Description.'),
        '#weight' => 100,

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.