2

I am building a D8 module that, for configuration, needs a list of webforms and their fields (just the entity reference fields, but I am not picky)

I can get the webforms with this:

  $webforms = \Drupal::entityTypeManager()->getStorage('webform')->loadMultiple(null);

But once I get the returned elements (Drupal\webform\Entity\Webform::__set_state), how can I fetch the elements in the webform referenced?

2
  • Are you trying to get the values a user entered? or just the field names the form contains?
    – No Sssweat
    Apr 20, 2019 at 12:20
  • Thanks, I was looking for the form's field names, and found the result myself.... eventually. Posted below. Apr 21, 2019 at 16:52

2 Answers 2

9

By trawling through the code, I found this handy function in the Entity code, and it works perfectly:

 $elements = $webform->getElementsDecodedAndFlattened();

This gives a nice array of the elements in the webform itself.

If you want the results of a submission, grab that entity and use:

$data = $entity->getData();

I hope this helps

1

The complete code for fetching the webform elements are listed below:

  $webform = Webform::load('webform_id'); //replace webform_id with the webform id
  if ($webform) {
    $view_builder = \Drupal::entityTypeManager()->getViewBuilder('webform');
    $elements = $webform->getElementsDecodedAndFlattened();
  }

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.