Score:-3

How to open an iframe with a different base URL for "Page Not Found" errors in Drupal?

cn flag

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:

<?php

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.

unusedspoon avatar
aq flag
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
Rayan Frem avatar
cn flag
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
Score:2
cn flag

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

Rayan Frem avatar
cn flag
Thank you for your answer. My issue is solved.
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.