I am working on a Drupal website and I need to implement a functionality where an iframe is opened with a different base URL whenever a "Page Not Found" error occurs. The goal is to display the content from a different website within the iframe.

I have tried implementing a custom module using the hook_preprocess_HOOK() and hook_page_attachments() hooks, but I'm encountering issues with the code not running when a "Page Not Found" error occurs.

Here is an example of the code I have tried:


use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

 * Implements hook_preprocess_HOOK() for page templates.
function iframe_error_preprocess_page(&$variables) {
  // Check if the current page is a "Page Not Found" error.
  $exception = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
  echo"<h1>INSIDE FUNCTION</h1>";
  if (!($exception instanceof NotFoundHttpException)) {
       echo"<h1>INSIDE 404</h1>";
    // Get the base URL of the iframe.
    $iframe_base_url = 'https://usj2.edu.lb';

    // Get the current URL.
    $current_url = \Drupal::request()->getUri();

    // Replace the base URL in the current URL.
    $iframe_url = str_replace('https://usj-dev.usj.edu.lb', $iframe_base_url, $current_url);

    // Build the iframe markup.
    $iframe_markup = '<iframe src="' . $iframe_url . '" width="100%" height="100%"></iframe>';

    // Assign the iframe markup to a variable for use in the page template.
   // $variables['iframe_markup'] = $iframe_markup;
    echo $iframe_markup;

Your help is appreciated.

  • 2
    There are several things wrong with your code. For 1 you're trying to add iframe HTML as a script tag into the <head> of the site (html_head). That's not going to work anywhere regardless of it being Drupal May 26, 2023 at 12:18
  • disregard the iframe, i sent an earlier version of the code here by mistake i'll just update it. I tested on a regular page by adding ! to the if condition and the iframe showed normally in addition to echo statements but on page not found nothing shows at all like it isn't getting the page to begin with
    – Rayan Frem
    May 26, 2023 at 12:23

1 Answer 1


You don't need to reinvent the wheel, core has already implemented 4xx status codes for the page template.

You can add the iframe to the main content region to show it in the default page template:

 * Implements hook_preprocess_HOOK() for page templates.
function mymodule_preprocess_page__404(&$variables) {
  $variables['page']['content']['iframe'] = [
    '#type' => 'inline_template',
    '#template' => '<iframe src="{{ url }}" width="100%" height="100%"></iframe>',
    '#context' => [
      'url' => 'http://www.example.com',

Or store the iframe URL in your own template variable and print the iframe tag in a custom page--404.html.twig.

See the change record https://www.drupal.org/node/2960810

  • Thank you for your answer. My issue is solved.
    – Rayan Frem
    May 28, 2023 at 16:39

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.