3

I'm trying to create a page through hook_menu so I can add my custom block with context instead.

function my_module_menu() {
    $items['my_page'] = array(
        'title' => 'my_page',
        'description' => 'my_page',
        'page callback' => 'my_function',
        'access callback' => true,
    );
    $items['my_page2'] = array(
        'title' => 'my_page2',
        'description' => 'my_page2',
        'page callback' => 'my_function',
        'access callback' => true,
    );
    return $items;
}

function my_function() {
    return '';
}

Is it the right way to do it? I have some bug from time to time, like the block goes missing on those pages. Could it be related to that?

EDIT: It turns out because I had configured a context path, it was messing with the item object, a normal block path is doing the trick!

  • Welcome to Drupal Answers! Please don't edit your question, if doing so invalidates any given answer. – kiamlaluno Mar 10 '15 at 11:25
0

No the menu items are fine.The problem seems with your block display configuration settings.

There is no permission with these menu items.So either give some loosely coupled permission 'access content' to these callbacks.Page callbacks name should be prefixed with modulename.

function my_module_menu() {
    $items['my_page'] = array(
        'title' => 'my_page',
        'description' => 'my_page',
        'page callback' => 'my_module_my_function',
        'access arguments' => 'access content',
    );
    $items['my_page2'] = array(
        'title' => 'my_page2',
        'description' => 'my_page2',
        'page callback' => 'my_module_my_function',
        'access arguments' => 'access content',
    );
    return $items;
}

function my_module_my_function() {
    return '';
}
  • I rename for this example but the callback prefix was my module name. I was also thinking about 'type' => MENU_NORMAL_ITEM, are you sure that access arguments is making my issue? thx – GwenM Mar 10 '15 at 11:09
  • If the module name is really my, then my_module_menu() would not be recognized as hook_menu() implementation, except in the case there is a my_module.module. – kiamlaluno Mar 10 '15 at 11:27
  • Let's assume that the module name is yop, then I called yop_menu() and yop_callbackfunction() – GwenM Mar 10 '15 at 11:35
  • Yup if the module name is yop then hook_menu should be yop_menu() {} The callback function name can be anything but as per drupal coding standards it should be prefixed with your module name i.e. yop in your case. – Naveen Valecha Mar 10 '15 at 12:54
  • the functions are called properly in my code, that wasn't the issue, I was thinking there might be something else like the return '' which could be buggy? – GwenM Mar 10 '15 at 14:19
0

If you want create menu item with hook_menu add

    'menu_name' => 'yourmenu-machinename', // This is the changed line.
    'type' => MENU_NORMAL_ITEM,

And as example

 $items['my_page'] = array(
    'title' => 'my_page',
    'description' => 'my_page',
    'page callback' => 'my_module_my_function',
    'access arguments' => 'access content',
     'menu_name' => 'yourmenu-machinename', // This is the changed line.
    'type' => MENU_NORMAL_ITEM,

); 

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.