1

I want to have a custom 404 page that displays some additional information. I need to display the requested path that was not found and some pages suggestions based on that path (this is based on some internal logic that I don't think is relevant to my question).

Here is what I did:

I defined the default 404 page to be page-not-found in admin/config/system/site-information

Then in my module I used hook_page_alter() to define my pages suggestions (if drupal_get_http_headers("status") is 404 Not Found), and the request_uri() to be shown on the 404 template page I created.

I have tried to save these values in different ways (session variable, ctools cache, everything I have tried leads to the same issue — see below).

In mymodule.module:

/** * Implement hook_menu * @return mixed */ function mymodule_menu() { $items['page-not-found'] = array( 'title' => '', 'page callback' => 'mymodule_page_not_found', 'file' => 'inc/functions.inc', 'access callback' => TRUE, ); return $items; } /** * Implement hook_theme */ function mymodule_theme() { return array( 'mymodule_page_not_found' => array( 'template' => 'templates/404', ), ); } 

In inc/functions.inc:

function mymodule_page_not_found() { return theme('mymodule_page_not_found'); } 

Problem is, if I request a non existing URL for example mysite.com/not-found-page-1 then request mysite.com/not-found-page-2 the information displayed on the 404 page template when I query the not-found-page-2 corresponds to the values I saved when requesting not-found-page-1.

Any idea why I always get the data of the previous query?

1
  • Yes and no. But for now, no. I'd first like to understand why my code behaves this way. Commented Aug 19, 2015 at 14:56

1 Answer 1

2

The trouble is, hook_page_alter() is called after your theme function is called. So if you're setting things there it's too late.

I don't know why you're doing it this way. Why not generate your suggestions in your page callback: mymodule_page_not_found(). Then pass them to the theme function.

Something like this:

/** * Implement hook_theme */ function mymodule_theme() { return array( 'mymodule_page_not_found' => array( 'template' => 'templates/404', // Not relevant to your question, but you should consider renaming this to mymodule-page-not-found because of a "bug" in D7; see discussion here: https://www.drupal.org/node/342350 'variables' => array('suggestions' => array()), ), ); } function mymodule_page_not_found() { // ... generate list of $suggestions ... return theme('mymodule_page_not_found', array('suggestions' => $suggestions)); } 
4
  • Yes, that's also what I thought regarding the hook_page_alter. Will try your suggestion. Commented Aug 19, 2015 at 15:09
  • Thanks a lot! That is way easier and works just fine! :) And thanks for the additional information too. Commented Aug 19, 2015 at 15:25
  • Quick question: I didn't add 'variables' => array('suggestions' => array()) to my code and it works. Why should I add that part? Commented Aug 19, 2015 at 15:27
  • 1
    It's the more proper way to do it as it offers a form of documentation for folks who want to know what the template expects. More practically, it provides a way for you to pass in default values in case anyone ever calls your theme function/template without providing a list of suggestions. Commented Aug 19, 2015 at 15:30

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.