Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a Drupal 7 module use up a .tpl.php template in the theme folder

I have created a module for Drupal 7 which has a hook_theme function that tells it to use usertemp.tpl.php template. I have the template placed in the module folder as well as the theme folder. The problem is that the function is ONLY picking up the template from the module folder but not from the theme folder. I have cleared the caches repeatedly and looked for previous answers but nothing helps. What could be the problem?

My code for the hook_theme looks like this:

function usuar_theme() {
  return array(
    'usuarbuild' => array(
        'variables' => array('profilesloaded' => array()),
        'template' => 'usertemp',
     ),
  );
}

The rest of the module code is this:

function usuar_menu() {
    $items['userx'] = array(
       'title' => 'User page',
       'description' => 'User page',
       'page callback' => 'usuar_exe',
       'access callback' => TRUE,
       'type' => MENU_CALLBACK,
     );
     return $items;
}


function usuar_exe($id) {
    $ar = array('uid' => $id, 'profilesloaded' => profile2_load_by_user($id));
    return theme('usuarbuild', array('collected' => $ar));
}

function theme_usuarbuild($variables) {
  return $variables['collected'];
}
like image 648
Carlos Muñiz Avatar asked Feb 24 '23 23:02

Carlos Muñiz


1 Answers

This is a tricky one, but.. your theme hook must match your template name. Weird, but I tested this on my local and it worked once I set it up that way. So.. change your hook_theme() to:

function usuar_theme() {
  return array(
    'usuarbuild' => array(
        'variables' => array('profilesloaded' => array()),
        'template' => 'usuarbuild',
     ),
  );
}

And change your tpl.php file to usuarbuild.tpl.php (or change everything to usertemp). Should work after that.

like image 117
hross Avatar answered Apr 07 '23 18:04

hross