0

So I know there is a way to specify a custom template for custom content types etc, but if I have a custom module where I am using hook_menu() to add custom pages with a page callback function, how can I specify that the pages I create use a custom template? This particular site is still on Drupal 6, so I would need to know the Drupal 6 method if it's possible

THANKS

0

If you have something like this:

function YOURMODULE_menu() {

  $items=array();

  $items['yourmodule/cool/path'] = array(
    'page callback' => 'YOURMODULE_cool_path_callback',
    // etc
  );

  // etc

  return $items;

}

then Drupal will utilize page-yourmodule-cool-path.tpl.php (eg, the /s are turned into -s) if it exists without any further action besides clearing your caches to pick it up.

In fact, if you create a page-yourmodule.tpl.php file, Drupal will use it for ANY path starting with yourmodule, eg, yourmodule/cool/path, yourmodule/cooler/path, etc., that doesn't have a more specific template created for it.

See Drupal 6 Template Suggestions for more info.

But, of course, if you have lots of paths that you want to use a single custom page template for that aren't so similarly defined, you can certainly use hook_preprocess_page() to suggest a non-standard template file:

function YOURMODULE_OR_THEME_preprocess_page(&$variables) {

  if (/* whatever is true */) {
    $variables['template_files'][] = 'page-custom';
  }

}

which would then have Drupal using page-custom.tpl.php for whatever was true in that if. Just realize if custom is defined as a path somewhere on your system, or is the start of a path, Drupal will use this template there as well.

Just realize somewhat obviously that whichever route you take, the page templates reside in your theme's directory and not with your module unless you use a registry alter:

function YOURMODULE_theme_registry_alter(&$theme_registry) {
  $theme_hook = 'page'; 
  $modulepath = drupal_get_path('module', 'YOURMODULE') . '/tpl/page';
  array_unshift($theme_registry[$theme_hook]['theme paths'], $modulepath);
}

which should cause Drupal to look in a tpl/page directory of your module for them but this brings up all sorts of other issues.

  • Ok so the funny thing is, that's what I thought and that is what I tried FIRST. I just created a template file page-MYCUSTOMPAGE.tpl.php, but no matter how many times I cleared the cache and no matter what I had in template.php, it would NOT get honored... So after messing with it, I decided to try a different alias and it worked! So here is what I found out and I'm not sure if I am crazy or not, but my original path ended in a number. Just for a test I created a path called "jobsearchresults2" and that was the one that would not work no matter what... – Erich H. Jan 27 '14 at 16:43
  • I then changed the path to "foo" and my template file to "foo.tpl.php" and it was fine. I think it was the fact that my path had a number in it. Again I could be crazy but a path with a number did not work but the path without one did. Anyway, I have it working now. THANKS GUYS! – Erich H. Jan 27 '14 at 16:44
  • Numbers as path elements are ignored/special within Drupal. Read the Template Suggestions link above for more information about them, but glad it's working. – Jimajamma Jan 27 '14 at 17:07
0

Your custom callback functions still use url aliases so you would just need to define a preprocess_hook that adds template files if that particular alias is being served. Here is a basic function you can use in your template.php file, modified from this handbook page:

function YOURTHEMENAME_preprocess_page(&$vars) {

    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));

    if (($alias != $_GET['q']) && ($alias == 'YOUR CUSTOM HOOK')) {;
      $template_filename = 'page' . '-' . 'YOUR CUSTOM TEMPLATE';
      $vars['template_files'][] = $template_filename;
    }

}

So, for example, if your menu hook was /trigger/awesome then the code could look like this:

function YOURTHEMENAME_preprocess_page(&$vars) {

    $alias = drupal_get_path_alias(str_replace('/edit','',$_GET['q']));

    if (($alias != $_GET['q']) && ($alias == 'trigger/awesome')) {;
      $template_filename = 'page' . '-' . 'awesome';
      $vars['template_files'][] = $template_filename;
    }

}

and the theme would now look to serve this page using the page-awesome.tpl.php file in your theme folder.

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.