Please save me! I installed a new Drupal 6.22 last week. I wrote a really simple example of hook_menu(), but it didn't work totally. I am really confused, hope someone help me please.

This is test.info

; $Id$
name = Test
description = Allows users to test nodes.
core = 6.x
package = Example

This is test.module

  /**
* Implementation of hook_menu().
*/
  function test_menu() {
     $items['admin/test'] = array(
       'title' => 'Test disp',
       'description' => 'This is first example.',
       'page callback' => 'drupal_get_form',
       'page arguments' => array('test_disp'),
        'type' => MENU_NORMAL_ITEM,
   );
    return $items;
  }

  function test_disp(){
    $output.='<table><tr><td><p>This is first example.</p></td></tr></table>';
    return $output;
  }

First of all, Administrator didn't view my menu item (test disp) until I added a new row 'path'=>'admin/test'.
Second, whatever I clicked muen item(test disp) or visited http://localhost/admin/test, it always show the problem.

Page not found
Welcome to the administration section. Here you may control how your site functions.
The requested page could not be found.

I have no idea why it would like that. I'll really appreciate it if you explain the problem for me.

Comments

juankvillegas’s picture

Some things:

Try adding to your $items:
'access callback' => TRUE,

But this will allow anyone to see the page. If this works, check access callback and access arguments

Other thing, test_disp must return a $form array, try changing it to

function test_disp(){
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'It works!'.
  );
  return $form;
}
Zilvador’s picture

I just wanted to add a comment after I spent many hours fighting with a hook_menu function that apparently did not work. After I had been working with the module for a long time and already added several pages, it seemed that the next path I wanted to register for a page had no effect when I added it to hook_menu. I started retracing my steps, looked for help in forums and so on and a _long_ time after I found that it all rooted in a function I had placed a while back at a completely unrelated place in the module file, which Drupal could not handle. So in the end it had nothing to do with hook_menu though it seemed like it.

I hope this might help others, since I have seen several versions of this problem while looking for advice. It is not really clear which PHP function can be used and which not some times, so trial and error can help you here.

Ah...and learning that clearing the cache in Drupal is actually enough to refresh the registered paths from hook_menu would also have been good to know earlier.

//Zilvador

tce’s picture

The page callback you are using is 'drupal_get_form', but the callback isn't a form, it's a page. So you would need to change to:

<?php
  function test_menu() {
    $items['admin/test'] = array(
      'title' => 'Test disp',
      'description' => 'This is first example.',
      'page callback' => 'test_disp',
   );

   return $items;
  }
?>

Also, although not really important, you don't need type => MENU_NORMAL_ITEM as that is assumed if no type is declared (it's the default).

The main thing you need to to do is clear the Drupal cache so it rebuilds the menu items in the database.