Using theme hooks to display a tpl.php on a menu path
The Node, Views and Panels modules can (and should!) handle most pages you need on a site, but sometimes you need complete control of a “hard coded” content area. You could create a new Page node with a Full HTML text format. Instead, how about putting the whole thing in code? Your work will be in version control and much more straightforward to maintain. Imagine editing HTML in a text editor rather than in the Drupal admin! So much better. Additionally, this method allows you to work more easily in a git-based workflow, such as those used by Pantheon or Acquia, without worrying about database changes.
Warnings for the less experienced Drupal site builder: This is specifically written to enforce the theme and logic separation functionality Drupal provides; no logic in the tpl files! If you can make a page in Views, it should be in Views. If you can make it in Panels, it should be in Panels. Do not use this method to recreate the wheel. If you find yourself writing large amounts of SQL queries in the page callback, then you are probably doing something wrong. Do it the “Drupal Way.” Your life will be less stressful in the long run. /rant
Ok! Let’s get to the code. First thing, make an info file for new module. Be sure to change the “variables” below to match your site and the page/app name.
name = Site Name - Page Name description = Creates the Page Name for the Site Name core = 7.x package = Site Name version = 7.x-1.0
Then make the module with hook_menu(), a page callback, hook_theme(), and a preprocess function.
Finally, make the template file.
<strong><?php print $hello;?> <?php print $world;?></strong>
Put the module and code in
$DRUPAL_ROOT/sites/all/module/custom/sitename_pagename/. Enable your new module and check the new page at:
Update 7/5/13: New article using this method to implement a template file for a custom block with an admin page to manage it: Instead, how about a Drupal block with a custom settings form?.
(Statically copied from previous site)
Jonathan-David … replied on June 5, 2013 - 4:34pm
Thanks man !!
Saxon replied on October 24, 2013 - 9:07am
Thanks a lot! Really helped out!
edgar-alexander replied on February 10, 2014 - 9:10am
Gracias, información muy buena. Thanks, information very good.
Tarun Nagpal replied on April 13, 2014 - 11:03pm
nikhilanant replied on February 20, 2015 - 12:54am
Error: syntax error, unexpected ‘(’, expecting ‘)’ in
brad replied on March 20, 2015 - 11:22am
What is the rest of this error message?
sunil kumar replied on April 10, 2015 - 11:26am
good hope, thankyou.
Colin replied on September 17, 2015 - 4:30am
:Hi, :Thanks for this great tutorial. :Wondering, how would one load an existing template within the current theme? :i.e page.tpl.php or page–front.tpl.php
I have a url which passes variables to the front page, which is why I am loading the page again but on a different url.
Thx in advance
brad replied on October 26, 2015 - 10:41pm
I don’t understand why you would reload the page to access variables… Can you explain further (if you haven’t already solved this issue)