For my drupal website, in my custom module (hr_payroll.module) I have the following to add URL handlers:
function hr_payroll_menu() {
$items['hr/payroll/employee/hours/overtime'] = array(
'title' => 'Overtime Submission',
'page callback' => 'hr_payroll_page',
'page arguments' => array('employee','hours','overtime'),
'access arguments' => array('access hr payroll'),
'type' => MENU_CALLBACK,
);
$items['hr/payroll'] = array(
'title' => 'Payroll',
'page callback' => 'hr_payroll_intro',
'access arguments' => array('access hr payroll'),
'type' => MENU_CALLBACK,
);
return $items;
}
On my site is a block that simply contains
echo(drupal_get_title());
If I go the URL hr/payroll, it shows the title 'Payroll'
BUT if I go to the URL hr/payroll/employee/hours/overtime it still shows 'Payroll' instead of the expected 'Overtime Submission'
So what am I totally misunderstanding about how the $title element of the menu item or the function drupal_get_title() work?
I believe you are using the hook_menu in the wrong way based on what you want to do.
First menu
$items['hr/payroll/employee/hours/overtime'] = array(
'title' => 'Overtime Submission',
'page callback' => 'hr_payroll_page',
'page arguments' => array('employee','hours','overtime'),
'access arguments' => array('access hr payroll'),
'type' => MENU_CALLBACK,
);
Are the arguments 'employee' 'hours' 'overtime' are static or they are dynamic ?? , and I see that page arguments supplied are the same ('employee','hours','overtime')
If they are dynamic use wildcard instead like
$items['hr/payroll/%/%/%'] = array(
'title' => 'Overtime Submission',
'page callback' => 'hr_payroll_page',
'page arguments' => array(2,3,4),
'access arguments' => array('access hr payroll'),
'type' => MENU_CALLBACK,
);
If the menu link "hr/payroll/employee/hours/overtime" is static , that means you don't need the access arguments provided as u already know what these values are on your page callback function.
And also why are you using type as MENU_CALLBACK.Is that by reason or by just random??
Related
here is my code
function custom_menu() {
$items['award/offer'] = array(
'page callback' => 'award_offer_email',
'page arguments' => array(1,3),
'type' => MENU_CALLBACK,
);
}
Here I have passed the url like http://dev.webroot.com/award/offer
but I am getting
The requested page "/award/offer" could not be found.
Any ideas?
first you need to return your menu items.
Other than that you also need to give access to your arguments.
Rewriting Your Example:
function custom_menu() {
$items['award/offer'] = array(
'page callback' => 'award_offer_email',
'page arguments' => array(1,3),
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
Now clear your cache and check it!
Did you clear cache after updating hook_menu()?
function custom_menu() {
$items['award/offer'] = array(
'page callback' => 'award_offer_email',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
return $items;
}
Clear cache menu (if you are using drush : drush cc menu) and refresh your page
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x
I have simple module:
function cabinet_menu() {
$items['cabinet'] = array(
'title' => 'cabinet',
'title callback' => 'cabinet_title',
//'title arguments' => array(1),
'page arguments' => array('cabinet_mysettings'),
'page callback' => 'cabinet_page',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
I want to pass to 'page arguments' a function called 'cabinet_mysettings'
function cabinet_mysettings() {
debug('call settings');
global $user;
$cabinet = user_load($user->uid);
return $cabinet;
}
function cabinet_page($cabinet) {
debug($cabinet);
}
In 'cabinet_page' debug shows only string "cabinet_mysettings".
Why does the menu hook not understand that the page arguments is not a function name but a sting?
UPD: devel module hook_menu:
$items['devel/reinstall'] = array(
'title' => 'Reinstall modules',
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_reinstall'),
'description' => 'Run hook_uninstall() and then hook_install() for a given module.',
'access arguments' => array('access devel information'),
'file' => 'devel.pages.inc',
'menu_name' => 'devel',
);
I think that 'devel_reinstall' is a function.
Does anyone know how such callbacks work?
Page arguments are not designed to do something behind the scenes. You use them to pass arguments to page callback by putting them in the url.
Putting string in page arguments as you did ('page arguments' => array('cabinet_mysettings')) causes that only that string is passed. There is no way around this. Refer to hook_menu documentation for more comprehensive explanation.
Therefore, in your case I would consider something like this:
function cabinet_menu() {
$items['cabinet/%'] = array(
'title' => 'cabinet',
'title callback' => 'cabinet_title',
'page arguments' => array(1),
'page callback' => 'cabinet_page',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function cabinet_mysettings() {
debug('call settings');
global $user;
$cabinet = user_load($user->uid);
return $cabinet;
}
function cabinet_page($my_param_name) {
$cabinet = cabinet_mysettings($my_param_name);
debug($cabinet);
}
Does this help?
I want my module to override the path was set by another module
Example:
Module A has register a path:
$menu['node/%id/test'] = array(
'title' => 'Test',
'page callback' => 'test_A',
'page arguments' => array(1),
'access callback' => 'test_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
)
Now I create module B and register the same path.
$menu['node/%id/test'] = array(
'title' => 'Test',
'page callback' => 'test_B',
'page arguments' => array(1),
'access callback' => 'test_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
)
Every request to this path
www.mysite.com/node/1/test
will route to module B not A.
What is the best way to override an existing path was set by other module?
You want to use an alter hook, hook_menu_alter():
function mymodule_menu_alter(&$items) {
$items['node/%id/test']['page callback'] = 'test_B';
}
Since you're just modifying an existing menu router definition, you only need to declare the part you want to change (e.g., the page callback function name). Note also $items is passed by reference, so you don't need to return anything.
I hope you can help me because I don't know how to call a node in a hook_menu in Drupal 7.
Is it possible ?
$items['basketfacile/planning'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'Test',
'description' => "description",
'page callback' => 'drupal_get_form',
'page arguments' => array('basketfacile_plannings_form'),
'access arguments' => array('access content')
);
This is my item menu who call a form, but I want to call a node form which allready exists in my Drupal installation. We can take Article for example.
Have you an idea ?
In fact, it's very easy to embed a node in an other page you can do that :
$items['menu/submenu'] = array(
'type' => MENU_LOCAL_TASK,
'title' => 'YOUR_TITLE',
'description' => "YOUR_DESCRIPTION",
'page callback' => 'node_add',
'page arguments' => array('YOUR_NODE_TYPE_NAME_MACHINE'),
'access callback' => 'node_access',
'access arguments' => array('create', 'YOUR_NODE_TYPE_NAME_MACHINE'),
'file' => 'node.pages.inc',
'file path' => drupal_get_path('module', 'node')
);
How can I add a tab into my personal profile (/users/my-name)?
I used this function, but nothuing shows up:
function tpzclassified_menu() {
$items['user/%user/kleinanzeigen'] = array(
'title' => t('Meine Kleinanzeigen'),
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
You're missing the page callback property:
function tpzclassified_menu() {
$items['user/%user/kleinanzeigen'] = array(
'title' => t('Meine Kleinanzeigen'),
'page callback' => 'tpzclassified_kleinanzeigen',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function tpzclassified_kleinanzeigen($account) {
return 'This is the Meine Kleinanzeigen page';
}
Replace tpzclassified_kleinanzeigen with the function name that generates the page.
Also, never use 'access callback' => TRUE: it's a huge security hole. I've changed that to use user_view_access(), which checks to the see if the user is allowed to view %user's profile. You could use user_edit_access() if you wanted to check to see if a user is allowed to edit %user's profile.