2

Is it possible to create template file for custom module created page that override default page theme only when the path is visited?

The code i tried so far is displaying my custom module result inside default page theme content area. I want to display result in my self created template file content area. Is it possible?

function mymodule_menu(){
    $items = array();   
    $items['mymodule'] = array(
        'title' => 'Mymodule title',
        'description' => 'Mymodule description',
        'page callback' => 'mymodule_page',
        'page arguments' => array(),
        'access callback' => TRUE,
        'type' => MENU_LOCAL_TASK,
    );
    return $items;
}

function mymodule_theme() {
    return array(
        'mymodule_theme_page' => array(
            'arguments' => array('mymodule' => NULL),
            'template' => 'page-mymodule', 
            'path' => drupal_get_path('module', 'mymodule').'/template',
        )
    );
}

function mymodule_page(){
    $mymodule = array('key'=>'Hello World');

    $output = theme('mymodule_theme_page', $mymodule);
    return $output;
}
3

The "most drupal" approach would be to place a copy of your template in your theme's directory named page-mymodule.tpl.php (eg, page-PATH.tpl.php) and be done with it. However, there are some cases where you may want to do this theming in a module, in which case you could do something along these lines in mymodule.module:

  // tell drupal that you want to use page-mymodule.tpl.php if you
  // are looking at www.yourhost.com/mymodule

  function mymodule_preprocess_page(&$vars) {

    if ($_GET['q']=='mymodule') {

      $vars['template_file']='page-mymodule';

      $vars['new_variable']=array('foo','bar','baz'); // $new_variable is now available in the template

    }

  }

  // tell drupal to look in .../mymodule/tpl/page
  // for page templates before looking anywhere else

  function mymodule_theme_registry_alter(&$theme_registry) {

    $theme_hook = 'page'; 
    $modulepath = drupal_get_path('module', 'mymodule') . '/tpl/page';
    array_unshift($theme_registry[$theme_hook]['theme paths'], $modulepath);

  }

Now, if you have page-mymodule.tpl.php in the directory .../mymodule/tpl/page and you rebuild your theme registry, easy if you have the Admin Menu module installed, otherwise, just clear all your caches, you should be on your way to using a page template in your module for that path.

(And, of course, in your page callback, you can return whatever content you want displayed in the content area of this template without using any of the theme functions you have in your original question.)

ADDITION: you can use the preprocess_page() hook to add any number of variables that are to be available to the template. So, in the above, you now have $new_variable available to use in there.

  • Thanks for sharing valuable information. Now only $content variable printing module result in tpl file but i want to send an array to tpl file from page callback. So i can parse it according to my need within tpl file. Is it possible without using hook_theme? – Vikas Naranje Mar 19 '13 at 7:25
  • I really don't understand what you mean by sending an array to the tpl file from the page callback. But I've added some more information above as a first guess... – Jimajamma Mar 19 '13 at 11:43

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.