1

I've created a custom module (called page_arg) simply for the purpose of registering a path and having it use a certain template file. I had this working with Drupal 6, but can't get it to work in Drupal 7. Basically, if I leave the page_arg_callback() function in the module, I get a 500 Server Error on every page in the site. If I remove it, I'm able to visit the path I'm trying to register, but it isn't using the template file I want it to use.

function page_arg_menu() {
  $items = array();

  $items['flashcards'] = array(
    'title' => 'Flashcards',
    'page callback' => 'page_arg_callback',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK
  );
  return $items;
}

function page_arg_preprocess_page(&$vars) {
    $url = urlSplit(); //custom function. trust me, this isn't the problem :)
    if($url[0] == 'flashcards'){
        $vars['template_files'][] = 'flashcards';
    }
}

function page_arg_callback($arg){
  return 'howdy';
}

As a secondary question, is this the best way to go about simply registering a path and pointing it to a template file? Seems a bit elaborate. Could I implement hook_menu() in my theme instead?

  • The code above didn't display an error on my installation (except for the urlSplit). The code in my answer is a sort-of reply to your last question as well, it's better to use hook_theme to point a callback to a template file. – Bart May 18 '11 at 15:48
4

Try this:

function page_arg_menu() {
  $items = array();

  $items['flashcards'] = array(
    'title' => 'Flashcards',
    'page callback' => 'page_arg_callback',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function page_arg_callback() {
  // Do stuff
  return theme('page_arg');
}

function page_arg_theme($existing, $type, $theme, $path) {
  return array(
    'page_arg' => array(
      'template' => 'page-arg',
      'path' => drupal_get_path('module', 'page_arg') . '/theme/templates',
    ),
  );
}

Create a file page-arg.tpl.php and put it in /theme/templates in your module (page_arg) directory.

| improve this answer | |

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.