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?

0

2 Answers 2

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,

); 
0

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

There is no permission with these menu items; give some loosely coupled permission (such as access content) to these callbacks. Function names should be prefixed with the module name to avoid clashes with other modules.

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 '';
}
6
  • 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, 2015 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.
    – apaderno
    Mar 10, 2015 at 11:27
  • Let's assume that the module name is yop, then I called yop_menu() and yop_callbackfunction()
    – GwenM
    Mar 10, 2015 at 11:35
  • 1
    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. Mar 10, 2015 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, 2015 at 14:19

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.