Score:-1

Custom module routing is going to page not found

in flag

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.'));
  }

}
leymannx avatar
ne flag
Where exactly you pass the webform ID?
apaderno avatar
us flag
The code does not show in which way is `custom_webform_template_page_callback()` called. The documentation comment describes it as page callback, but in Drupal 9 a page callback is a controller method, not a plain function.
id flag
webform_id is missing from the route path.
user3480098 avatar
in flag
Thank you all so much, i've made some changes to my module however i'm still getting a page not found, am I still missing something? I've updated my original post with all of my code. The webform ID is 'contact' that i want to display on /custom-webform
id flag
Can you confirm that 1) You are browsing the site on path /custom-webform and 2) that you rebuilt the site caches?
user3480098 avatar
in flag
yep I browse to /custom-webform and yep rebuilt caches (several times) and tried in incognito mode
Score:0
in flag

I changed my displaywebform function to just this and it now works;

  public function displayWebform() {


      $webform = \Drupal::entityTypeManager()->getStorage('webform')->load('contact');
      $webform = $webform->getSubmissionForm();
      
      return $webform;
    }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.