Unifying the questions below, I want to check if the problem and solution is correct:

Problem: I want to create a page with hook_menu from my custom module. This page will have a different template to the other pages. Some of the dynamic content of the page will be retrieved by my custom module.

Solution: the code below, with these convoluted steps:

  1. hook_menu with callback function
  2. callback function returns empty array.
  3. Based on path, hook_preprocess_page retrieves data and send to page.
  4. theme suggestion is set, based on path.

What I don't understand is why 3. and 4. cannot be accessed directly from callback function, avoiding to guess what page we are accesing via path.


function hook_menu(){
    $items['product-detail'] = array(
       'page callback' => '_hook_product_detail',
       'type' => MENU_CALLBACK,
       'access callback' => TRUE,

function _hook_product_detail(){
   return array("#markup" => "");
} // function

function hook_preprocess_page(&$variables) {
   $cp = current_path();
   $cp_arr = explode("/", $cp);
   if($cp_arr[0] == "product-detail"){
       $variables['theme_hook_suggestion'] = 'page__product_detail';
       // ... get data to populate var1, used in page--product_detail.tpl.php
       $variables["var1"] = $var1;
   } // if


Questions unified: How to Create a single page within a module and set page variables? and https://drupal.stackexchange.com/questions/177604/how-can-i-put-the-results-from-my-module-into-a-page-tpl-php

  • Error correcction: what must be used is what appears in bold after 3 and 4. Direct use of the callback function used in hook_menu.
    – Cesar
    Commented Oct 19, 2015 at 7:27
  • Have you reviewed drupal.org/node/223440? Commented Oct 20, 2015 at 10:19
  • Yes, assignament $variables['theme_hook_suggestion'] = 'page__product_detail'; could be used in template.php. I have working sites using the approach. I just want to make it work from the module file, and from the hook_menu or a callback function used by it. It is the logical approach, and I must be missing something very simple or just Drupal makes convolute what could be enormously simple. It is the part $cp = current_path(); $cp_arr = explode("/", $cp); what seems terribly wrong, even if it works.
    – Cesar
    Commented Oct 20, 2015 at 14:15
  • In template.php approach, I use a dummy node of a specific content type, and use node->nid to put the logic of data retrieval, theme_suggestion, etc. That seems weird, also, even if it works. I will have to strip a module to see how they get it.
    – Cesar
    Commented Oct 20, 2015 at 14:22
  • 1
    Please avoid asking three questions about the same topic in rapid succession. Take your time to try find the solution, and to write a comprensible question. It's not that you ask a new question for every new detail you find out.
    – apaderno
    Commented Oct 21, 2015 at 16:11

1 Answer 1


You can add variables from the page callback. I imagine the placement of the variable in your $vars will depend on your theme.

This works for me using a responsive_bartik sub theme and panels. The my_var exists in $vars['page']['content']['system_main']['my_var'].

my_module_menu() {
  $items['test-empty'] = array(
    'page callback' => 'my_module_page_view',
    'page arguments' => array('This is my var'),
    'theme callback' => 'my_module_theme',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,

my_module_page_view($var) {
  $output['#markup'] = 'Your markup or just an $output render array';
  $output['my_var'] = $var;
  return $output;

my_module_theme() {
  return your_theme

my_module_page_preprocess($vars) {
  // Path to my_var may differ from theme to theme and by set up.
  $vars['page_level_var'] = $vars['page']['content']['system_main']['my_var'];

Now your $page_level_var should be available in your tpl.

You shouldn't need the theme callback but I could be wrong. (It would have been a pain not to use it with my current set up.)

  • That is what I needed -- the location of the variable value. Instead of being directly accesible via $myvar in the page.tpl.php, it is "buried" in page-->content-->system_main-->myvar. Thanks!!!
    – Cesar
    Commented Oct 21, 2015 at 14:32

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.