0

I'm working on a Drupal 8 project, when trying to load a webform programmatically in a controller, the webform is rendered correctly but when data has been submitted no values founded on webform results page (still always empty).

Injecting the form in a paragraph as block and then submit data work perfectly, also submitting data from the webform node page works as it should (We can find data on form results page).

Here is my code:

MyController.php

namespace Drupal\my_module\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\webform\Entity\Webform;

class MyFormPage extends ControllerBase {

  public function content() {
      $webform = Webform::load('my_webform_id');
      $view_builder = \Drupal::service('entity_type.manager')->getViewBuilder('webform');
      $webform        = $view_builder->view($webform);
      return [
        '#theme'   => mycontroller_page',
        '#content' => [
          'title'     => t('I am a page from your controller'),
          'form'      => $webform,
        ],
      ];
  }
}

mycontroller-page.html.twig

<article>
  <h1>{{ content.title }}</h1>
  <div>
    {{ content.form }}
  </div>
</article>

Any idea about this behaviors? Thanks

2

@see How to embed a Webform?

I recommend using something like...

return [
  '#theme' => mycontroller_page',
  '#content' => [
    'title' => t('I am a page from your controller'),
    'form' => [
      '#type' => 'webform',
      '#webform' => 'my_webform_id',
    ],
  ],
];

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.