1

I can't figure out why this module is not working. What is wrong with it?

/* hook_menu() */
function profilepage_menu() {
  $items['p'] = array(
    'title' => 'Profile page',
    'page callback' => 'profile_exe',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/* Callback function */
function profile_exe($id) { 
  $ar = array('uid' => $id, 'profilesload' => profile2_load_by_user($id));

  return theme('profilebuild', array('collected' => $ar));
}

/* Custom theme function. */
function theme_profilebuild($variables) {
  return "";
}

/* hook_theme() */
function profilebuild_theme() {
  return array(
    'profilebuild' => array(
      'variables' => array('profilesload' => array()),
      'template' => 'profilepage',
    ),
  );
}

The module is installed and a page called profilepage.tpl.php is in my theme directory AND in the module directory.

The output I'm expecting should be the text in the profilepage.tpl.php page along with the contents of the $variables array which I plan to use in my template.

However, when I open http://example.com/?q=p in my browser, the page loads without errors but the expected content is not there.

What am I doing wrong?

7
  • Have you cleared your theme registry and/or site cache? – user7 Jun 4 '11 at 20:34
  • yes I did! I think I made progress - the hook_theme has to have the same name as the module. but now it doesn't pick up the profilepage.tpl.php that is in the theme folder. it picks up the one in the module folder. any ideas? – Carlos Muñiz Jun 4 '11 at 21:43
  • 1
    @Charlie Any time you add a .tpl file to your theme, you need to clear your cache again. If the site picks up the module's .tpl file, it should pick up the theme .tpl file after you clear the theme registry cache – user7 Jun 4 '11 at 21:47
  • yes, I know it should, but i'm clearing up the cache and it doesn't pick it up. I have had to place a relative path to the .tpl file in the themes folder, and that's the only way it picks it up. that's very weird isnt it? – Carlos Muñiz Jun 4 '11 at 22:00
  • 1
    @Charlie can you update your question to what your code looks like now? – user7 Jun 4 '11 at 22:17
0

The problem I can see with your code is the file name for the template file that you declare in your hook_theme() implementation. The file name for the template file should be the theme identifier used as array index in the array returned by hook_theme() (in your case, "profilebuild"); where the identifier uses underscores, the template name should use hyphens.

As example of how the template files are named, see the following hook_theme() implementations for Drupal core modules.

function book_theme() {
  return array(
    'book_navigation' => array(
      'variables' => array('book_link' => NULL), 
      'template' => 'book-navigation',
    ), 
    'book_export_html' => array(
      'variables' => array('title' => NULL, 'contents' => NULL, 'depth' => NULL), 
      'template' => 'book-export-html',
    ), 
    'book_admin_table' => array(
      'render element' => 'form',
    ), 
    'book_title_link' => array(
      'variables' => array('link' => NULL),
    ), 
    'book_all_books_block' => array(
      'render element' => 'book_menus', 
      'template' => 'book-all-books-block',
    ), 
    'book_node_export_html' => array(
      'variables' => array('node' => NULL, 'children' => NULL), 
      'template' => 'book-node-export-html',
    ),
  );
}

function aggregator_theme() {
  return array(
    'aggregator_wrapper' => array(
      'variables' => array('content' => NULL), 
      'file' => 'aggregator.pages.inc', 
      'template' => 'aggregator-wrapper',
    ), 
    'aggregator_categorize_items' => array(
      'render element' => 'form', 
      'file' => 'aggregator.pages.inc',
    ), 
    'aggregator_feed_source' => array(
      'variables' => array('feed' => NULL), 
      'file' => 'aggregator.pages.inc', 
      'template' => 'aggregator-feed-source',
    ), 
    'aggregator_block_item' => array(
      'variables' => array('item' => NULL, 'feed' => 0),
    ), 
    'aggregator_summary_items' => array(
      'variables' => array('summary_items' => NULL, 'source' => NULL), 
      'file' => 'aggregator.pages.inc', 
      'template' => 'aggregator-summary-items',
    ), 
    'aggregator_summary_item' => array(
      'variables' => array('item' => NULL), 
      'file' => 'aggregator.pages.inc', 
      'template' => 'aggregator-summary-item',
    ), 
    'aggregator_item' => array(
      'variables' => array('item' => NULL), 
      'file' => 'aggregator.pages.inc', 
      'template' => 'aggregator-item',
    ), 
    'aggregator_page_opml' => array(
      'variables' => array('feeds' => NULL), 
      'file' => 'aggregator.pages.inc',
    ), 
    'aggregator_page_rss' => array(
      'variables' => array('feeds' => NULL, 'category' => NULL), 
      'file' => 'aggregator.pages.inc',
    ),
  );
}

Drupal 7 implementations of that hook is slightly different from the implementation done in Drupal 6, but the "template" index is used in the same way in both the versions.

function block_theme() {
  return array(
    'block_admin_display_form' => array(
      'template' => 'block-admin-display-form', 
      'file' => 'block.admin.inc', 
      'arguments' => array('form' => NULL),
    ),
  );
}

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.