1

I am trying to create a module in Drupal 6.22, using the following code.

function example_video_perm() {
  return array("video access");
}

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

  $items['video/%nid'] = array(
    'title' => '',
    'page callback' => 'video_load',
    'access arguments' => array('video access'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

function video_load($nid) {
  print_r($nid); 
}

Whenever I am trying to access http://example.com/video/nid, I get a 404 error. Please help.

2
  • Did you assign the permission?
    – mpdonadio
    Aug 12, 2011 at 13:54
  • Yeah, but 404 mistake
    – xurshid
    Aug 12, 2011 at 14:08

4 Answers 4

0
function example_video_menu() {
    $items = array();
    $items['video/%'] = array(
        'title' => '',
        'page callback' => 'video_load',
        'page arguments' => array(1),
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );
    return $items;
}

function video_load($nid) {
    print_r($nid); 
}
2

The problem is that the path associated to your menu item is using a wildcard ("%nid"), but your code is not defining an auto-loader for that wildcard.

  • For a wildcard such as "%nid" Drupal is looking for a function called nid_load(), which doesn't exist; if you want to use video_load(), then the placeholder should be "%video" and not the one you are using. If that part of the path is a node ID, then you should use "%node" that would cause Drupal to use node_load(), which is a function defined from the Node object and that returns a node object for the node ID passed as argument.
  • The auto-loader function should return an object or an array. If the auto-loader function is not returning a value, then Drupal would act as if the returned value is FALSE, which is the value that causes Drupal to return to the user the "page not found" error, as it understands the value as meaning "I didn't found what the user is looking for." Return an object (which is what node_load() does), or an array, and the user will not see the "page not found" error anymore.

In your case, the URL for the menu item defined from the module is something similar to http://example.com/video/1, or http://example.com/video/2, where 1 and 2 are (I presume from your code, as you use "%nid" as wildcard, which should mean "node ID") the node IDs of existing nodes.
Take as example the URL for the menu item associated with the node view page, which is "node/%node"; you invoke that page with, e.g., http://example.com/node/1, and Drupal shows you the content of the node with node ID equal to 1.

If you don't want to use an auto-loader, then your menu item should use a path such as "video/%" and Drupal would not look for an auto-loader function. What I reported about the URL to use to invoke the page served from your module is still valid, except that the URL could be http://example.com/video/asdfsdfwer, http://example.com/video/2, http://example.com/video/azder54gfg because the % in "video/%" matches any string.

Bart is correct in saying that using "%nid" as placeholder in the path, when the page callback, the access callback, or the title callback doesn't get any argument derived from that placeholder, doesn't make sense.

For more information, you can look at the documentation for hook_menu(); Wildcard Loader Arguments, Wildcard usage and core's wildcards, Dynamic argument replacement (wildcard), Wildcard, ancestors and fitness; and, as general information on how Drupal menu system work, Drupal menu system (Drupal 6.x).

1

The page callback should return the content of the page as a string. See menu_execute_active_handler about how the page callback is invoked and index.php about what is done with the result of menu_execute_active_handler.

This doe not explain the 404, but it's the next trap you will fall into.

0

You forgot page arguments!

"page arguments": An array of arguments to pass to the page callback function, with path component substitution as described above.

3
  • I have set argument to the menu array 'page arguments' => array(1), But still drupal giving me 404.
    – xurshid
    Aug 12, 2011 at 13:26
  • Did you clear the cache?
    – Bart
    Aug 12, 2011 at 13:29
  • And I have cleared cache, but 404
    – xurshid
    Aug 12, 2011 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.