0

I have one page mortgages/%/view that needs to have it's own custom theme. I have the HTML created and it's ready to go. In my 'mortgage' module I have a mortgage.tpl.php file with this HTML in it.

My hook_menu looks like this:

function mortgage_menu() {

  $items = array();

  $items['mortgage/%/view'] = array(
    'page callback' => 'mortgage_callback',
    'page argument' => array(3), //pass 3rd argument of url -- 1. drupal 2. mortgage 3. %
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
 }

Then my callback function and mortgage_theme function look like this:

function mortgage_callback() {
 return theme('mortgage');
}

 /*
 * Implementation of hook_theme().
 */

function mortgage_theme(){
  return array (
    'mortgage' => array (
      'template' => 'mortgage' ,
      'arguments' => array(),
    ),
  );
}

This successfully calls my mortgage.tpl.php file into my mortgage/%/view page. HOWEVER, it only displays in the middle of my "content" section instead of correctly overriding the theme. Obviously this isn't correct.

How would I get this mortgage.tpl.php file to be THE theme file for this specific page?

e/ Mortgage is NOT a node. It's a custom module which allows users to get a full page view of mortgage details.

4
  • The HTML from mortgage.tpl.php appears to be missing from your post. Commented Aug 20, 2015 at 17:53
  • @othermachines - I didn't include it because it's a lot of code. I can put it in a GIST but it's essentially the bootstrap theme I use copied over and removed the header/mainmenu from it was all. I can post it all but just seemed irrelevant because it all posts just in the middle of the page and keeps the default template. It's like it's applying both templates o.O
    – Exziled
    Commented Aug 20, 2015 at 18:20
  • Did you look at drupal.org/project/themekey module? Commented Aug 20, 2015 at 18:46
  • @ar7max can this be used to just remove certain parts of a current theme? I don't really want to make a whole new theme and change the entire theme.
    – Exziled
    Commented Aug 20, 2015 at 19:01

2 Answers 2

2

In your module

function custom_theme($existing, $type, $theme, $path){
      return array(
        'mortgage_page' =>  array(
          'template' => 'theme/mortgage',
          'render element' => 'page'
        ),
      );
    }

function custom_preprocess_page(&$variables, $hook) {
  if ( preg_match("/mortgage\/.*\/view/", drupal_get_path_alias($_GET['q'])) ) {
   $variables['theme_hook_suggestions'][] = 'mortgage_page';
  }
}

Check for path alias to be correct and change theme_hook_suggestions to your theme.

1
  • 1
    Awesome! I edited yours a bit but custom_preprocess_page() was exactly what I was missing. I wish I could upvote it but I'm 1 shy lol e/ jk I have 16 now since I accepted the answer!
    – Exziled
    Commented Aug 20, 2015 at 21:51
0

This working as designed, if you want to override the whole page markup, you might consider custom page.tpl.php, Also you would use template_preprocess_page to add theme_hook_suggestions to let that page pickup your custom_page.tpl.php

Yourtheme_preprocess_page(&$vars) {
   if(something) {
      $vars['theme_hook_suggestions'][] = 'page_your_custom_page_tpl';
   }
}
2
  • Can I just load the page.tpl.php in my module instead of the mortgage.tpl.php? I guess this would completely override the standard page.tpl?
    – Exziled
    Commented Aug 20, 2015 at 19:03
  • well yes, but you need to tell Drupal to pick it up by adding "theme_hook_suggestions"
    – sam2de
    Commented Aug 20, 2015 at 20:24

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.