1

On my D7.80 I have developed a custom module:

    function wrong_answer_menu() {
    dd('it entered in hook menu');
  $items['wrong-answer/%id'] = array(
    'title' => t('Wrong Answer Dashboard'),
    'page callback' => 'wrong_answer_dashboard',
    'page arguments' => array(1),
    'access callback' => TRUE,
    'type' => MENU_LOCAL_TASK,
  );

  return $items;
}

/**
 * Callback: hook_menu.
 */
function wrong_answer_dashboard($node_param=NULL) {
dd('it entered in dashboard');
....
}

Everything was fine, I was developing the menu nicely accessing the mydomain.com/wrong-answer and everything was fine.

Then I wanted to add permissions to my custom module, so that only the admin can access this page hence I added the wrong_answer_permission function and in the dashboard function I checked if the user had permission to access the page

/**
 * Implements hook_permission().
 */
function wrong_answer_permission() {
  return array(
    'wrong_answer_' => array(
      'title' => t('View the wrong answer'),
    ),
  );
}

    function wrong_answer_dashboard($node_param=NULL) {
     dd('it entered in dashboard');
     if (!user_access('wrong_answer_admin')) {
      return;
     }
    ....
    }

At this stage, when I accessed the page mydomain.com/wrong-answer I could still access it even though I did not assign the permission to it. Then I cleared the cache from the Performance menu via the admin/config/development/performance, and then I got the error message

The requested page "/wrong-answer/717" could not be found.

I get the same error for acessing just mydomain.com/wrong-answer.

Then I thought it was all fine, I added the permission wrong_answer_admin to the admin role, but then I still got the same error "The requested page .... could not be found.

Afterwards, I:

  • cleared the cached
  • removed the code that I had added on the permission and checking the user access
  • rebuild the registry_file by emptying the table and running the update.php
  • assigned the permission to the wrong_answer_admin to all the roles, including anonymous role
  • deleted the wrong_answer_admin row in the permissions table in the database
  • disabled and re-enabled by custom module

but nothing worked, I still get the error "The requested page cannot be found".

Then I noticed the strangest thing, that whenever I would clear the cache at, I would would get an entry in the drupal_debug.txt

it entered in hook menu

I do not get this entry in the drupal_debug.txt when accessing the wrong-answer page, I only get it when clearing the cache.

Could you please help me understand what is going on? why can't I access the wrong-answer page anymore?

Many thanks

2
  • What does your menu definition look like after you added the permission?
    – Jaypan
    Commented May 21, 2021 at 5:32
  • Are you sure you defined a wrong_answer_admin permission?
    – avpaderno
    Commented May 21, 2021 at 11:08

2 Answers 2

0

An answer to part of your question:

Then I noticed the strangest thing, that whenever I would clear the cache at, I would would get an entry in the drupal_debug.txt

it entered in hook menu

I do not get this entry in the drupal_debug.txt when accessing the wrong-answer page, I only get it when clearing the cache.

If Drupal built the menu for every page load, it would result in heavier page loads. As such, hook_menu() is only called after the cache is cleared, as the menu cache is rebuilt the first time it is needed and no cache exists. Once it's been cached, hook_menu() does not get called anymore.

0

It seems like that the problem will be in the menu item definition:

$items['wrong-answer/%id'] = array(

I do not think you can use random string in the wildcard placeholder. According to the documentation, you can:

  1. Use plain wildcard (%) - this will pass the ID directly to your function.
  2. Use auto-loader wildcard (for example %node) - but the name must reference an existing loading function. In this case the node_load() function will be called and the node object will be passed to your function.
  3. Define %wildcard_to_arg() function (see documentation).

It is very likely that you do not have id_load() function in your code, so this should be the reason why this is not working. From the documentation:

... if a node with a node ID of 999 does not exist, 
the menu routing system will return a 404 error in this case...

If you change it to this, it should work correctly:

$items['wrong-answer/%'] = array(

See hook_menu documentation for additional information: https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu/7.x

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.