0

Here's the implementation of hook_menu() in my theme, "myTheme":

function myTheme_menu() {
    $items = array();
    $items['ajax'] = array(
        'title' => 'AJAX',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK
    );
    $items['blog'] = array(
        'title' => 'sonia blog',
        'access arguments' => array('access content'),
        'type' => MENU_CALLBACK
    );
    return $items;
}

example.com/ajax is working fine, but example.com/blog is returning me a "page not found" error. I have cleared my cache.

Do you have any idea about what is going wrong?

EDIT
One of the commenters suggested that I need a page_callback property. However, the documentation on hook_menu does not say that it's required. Regardless, I tried adding this callback but still no luck. Here's the code with it:

function myTheme_menu() {
    $items = array();
    $items['blog'] = array(
        'title' => 'sonia blog',
        'access arguments' => array('access content'),
        'page callback' => 'myTheme_callback_blog',
        'type' => MENU_CALLBACK
    );
    return $items;
}

function myTheme_callback_blog(){
    return '';
}
9
  • 1
    Are you sure /blog is not already defined by another module ? Did you try another path, just for checking ?
    – Countzero
    Commented Mar 2, 2012 at 17:31
  • 3
    /blog is used by the core Blog module
    – Laxman13
    Commented Mar 2, 2012 at 17:34
  • 1
    Are you defining this in a theme or a module?
    – mpdonadio
    Commented Mar 2, 2012 at 17:51
  • 5
    Both your menu items are missing the page callback property, see hook_menu() for more details
    – Clive
    Commented Mar 2, 2012 at 17:51
  • 1
    @maxedison It's only not required if the item can inherit the page callback from a parent item. So if the path 'page' has a page callback then the path page/sub-page will inherit it. Otherwise its required or Drupal has no function to run to get the page content :) Not sure why the ajax path works though so maybe that's not quite right. As others have said though, blog is already a defined path so you need to alter it in hook_menu_alter() rather than redefine it
    – Clive
    Commented Mar 4, 2012 at 14:12

2 Answers 2

4

I believe you cannot define a hook_menu() implementation in a theme; it should be defined in a module.

When you make changes to a hook_menu() implementation, you need to ensure that you rebuild the menu router, as well. To that end you can manually look at your database's "menu_router" table to see if the callback is being registered.

1
  • This is correct; a theme can implement hook_menu_alter() on Drupal 7, but it cannot implement hook_menu().
    – apaderno
    Commented Mar 28, 2012 at 17:53
0

You are trying to implement these in a theme, which will not work; you need to add these in a module.

See How to use hook_user_login and hook_user_logout to output content to the page.

Your Answer

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

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