1

I have a hook_menu like that:

function mymodule_menu() {
    $items['mymodule-test'] = array(
        'page callback' => '_mymodule_test_callback',
        'access callback' => TRUE
    );
    return $items;
}

and the callback function:

function _mymodule_test_callback()
{
    // processing
    return $html;
}

I googled around but i cant find how to force the page template inside my _mymodule_test_callback function.

Edit: why i cant (or dont feel comfortable with) url-based suggestion

Url-based suggestion are easy to implement, however they can not completely fit my needs.

The template I have to use is determined in the _mymodule_test_callback function, according to very various aspects, for example:

  1. Does exists a node of type 'foo', and does the field 'abc' value's be 'xyx'? => choose template 'page-mymodule-foo.tpl.php'
  2. The node does exists, but the field 'abc' value is empty? => Choose temlate 'page-mymodule-bar.tpl.php'
  3. The node does not exist at all, but the user has the 'bimbumbam' role? => Choose template 'page-mymodule-foobarz.tpl.php'

...and so on.

I know this is a rare behavior, and that i could redirect instead of switching template, but actually the function body is not big and i prefer to do this way.

Is not possible?

  • if my understanding of you query "to use a custom page template other than page.tpl.php for this specific menu callback" is right, you could use page-internal_path.tpl.php which in this case should be page-mymodule-test.tpl.php based on my understanding of Drupal 6 template suggestions, please let me know if i've misinterpreted the question. – optimusprime619 Aug 28 '12 at 12:55
  • Silly me, off course using page-<path>.tpl.php is the best answer when the custom page template is needed for a single page callback. – Pierre Buyle Aug 28 '12 at 13:02
  • @PierreBuyle You're right, but the problem comes when i have to choose the template file according to what happen in the _mymodule_test_callback() function. For example, if a determinate node exist and has some attributes, i can choose 'foo' template, else, 'bar' template. If the node doesnt exist at all and the user have the 'cippalippa' role, then i have to choose the 'foobar' template.. and so on ;) – Strae Aug 28 '12 at 13:14
  • @optimusprime619 You understood right my question, but my question lack of details.. my bad, i update the question ;) – Strae Aug 28 '12 at 13:15
  • have you checked the possibilities of enforcing such conditions in your module_preprocess_page() with &$variables like mentioned in below answer? In similar situations I've done such stuff in my theme_preprocess_page() with function calls to modules to determine decision making conditions, one bad experience with White screen of death when calling function of module which is disabled made me use function_exists and module_exists before calling a function to be on safer side, but it goes without saying that though my approach worked for me its muddy and not recommended. – optimusprime619 Aug 28 '12 at 15:17
1

You cannot directly change the page template from a page callback. You can either bypass the full page rendering by printing your page content from your page callback. Or you can use a global variable to trigger addition of template suggestion in hook_preprocess_HOOK implementation.

function _no_template_page_callback() {
  // ... processing ...
  // Print the result, and bypass the page template (so no blocks, no CSS, no JavaScript, etc.). 
  print $html;
}

function _template_suggestions_callback() {
  global $MODULE_page_template_suggestions;  
  // ... processing ...
  // Set flag to trigger usage of our custom page template. 
  $MODULE_suggest_page_template = $suggestions;
  return $html;
}

function MODULE_preprocess_page(&$variables) {
  global $MODULE_page_template_suggestions;
  if (!empty($MODULE_page_template_suggestions)) {
    // Use template suggestions set from _template_suggestions_callback the module.
    foreach ($MODULE_page_template_suggestions as $suggestion) {
      $variables['template_files'][] = $suggestion;
    }
  }
}
  • nice one, have a doubt on the precedence...will the template suggestion in modulename_preprocess_page be overidden if there's a similar condition for the OP's template suggestion also exists in theme_preprocess_page ? – optimusprime619 Aug 28 '12 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.