I have a custom module that i'd like to display my webform on a certain page but it keeps sending me to a 'Page not found'.
folder structure is;
modules/custom/custom_webform_display
-custom_webform_display.info.yml
-custom_webform_display.module
-custom_webform_display.routing.yml
--src
---Controller
----CustomWebformDisplayController.php
--templates
---custom-webform-display-page.html.twig
info.yml
name: 'Custom Webform Display'
type: module
description: 'Displays a webform in a custom page template.'
core_version_requirement: ^9 || ^10
package: Custom
dependencies:
- webform:webform
.module
<?php
use Drupal\Core\Render\Markup;
/**
* Implements hook_theme().
*/
function custom_webform_template_theme($existing, $type, $theme, $path) {
return [
'custom_webform_template_page' => [
'variables' => [
'webform' => NULL,
'webform_content' => NULL,
],
'template' => 'custom-webform-template-page',
'path' => $path . '/templates',
],
];
}
/**
* Page callback for the custom webform template page.
*/
function custom_webform_template_page_callback($webform_id) {
$webform = \Drupal::entityTypeManager()->getStorage('webform')->load($webform_id);
if ($webform) {
// Ensure the webform content is a render array before rendering.
$webform_content = $webform->getSubmissionForm()->render();
$webform_content = Markup::create($webform_content);
$build = [
'#theme' => 'custom_webform_template_page',
'#webform' => $webform,
'#webform_content' => $webform_content,
];
return $build;
} else {
// If the webform with the given ID does not exist, return a 404 page.
throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
}
}
.routing.yml
custom_webform_display.display:
path: '/custom-webform'
defaults:
_controller: '\Drupal\custom_webform_display\Controller\CustomWebformDisplayController::displayWebform'
requirements:
_permission: 'access content'
.page.html.twig
{#
/**
* @file
* Default template file for displaying a webform in a custom page template.
*
* Available variables:
* - webform: The rendered webform.
*/
#}
<div class="custom-webform-display">
{{ webform }}
</div>
Controller.php
<?php
namespace Drupal\custom_webform_display\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller for displaying a webform in a custom page template.
*/
class CustomWebformDisplayController extends ControllerBase {
/**
* Displays the webform in a custom page template.
*
* @return \Symfony\Component\HttpFoundation\Response
* The rendered webform.
*/
public function displayWebform() {
// Load the webform by its ID. Replace 'your_webform_id' with the actual webform ID.
$webform_id = 'contact';
$webform = \Drupal\webform\Entity\Webform::load($webform_id);
// Check if the webform exists and is open.
if ($webform && $webform->isOpen()) {
// Render the webform.
$form = \Drupal::formBuilder()->getForm($webform);
$rendered_form = \Drupal::service('renderer')->render($form);
// Create a custom page template and replace 'custom_webform_display_page' with your template name.
$output = [
'#theme' => 'custom_webform_display_page',
'#webform' => $rendered_form,
];
return new Response(\Drupal::service('renderer')->render($output));
}
// If the webform doesn't exist or is closed, display an error message or redirect to another page.
return new Response($this->t('The webform is not available at the moment.'));
}
}