In drupal 7 I'm trying to give permissions of using a node if you are member of the same group as the node.
I want to use hook_menu and define my custom access check function. To this function I sent the nid as a parameter.
This is what I have got now, and I realy don't see why it is not working:
function modulename_pdf_menu() {
$items['pdf/node/%'] = array(
'page callback' => '_modulename_pdf',
'access callback' => '_modulename_pdf_access_check',
'access arguments' => array(2),
'type' => MENU_CALLBACK
);
return $items;
}
function _modulename_pdf_access_check($nid) {
echo $nid;
die();
}
I assume this should print my node id to the screen and stop. But it is still running the logic defined in _modulename_pdf. Any idea what I'm missing here?
Thanks in advance for your reply.
You are right, clear the cache and check it.
Related
I am sending a link out via email to users when they register that they must click and it will automatically direct and log them into the site.
I'm getting a 404 error when trying to access this link.
Example email sent out:
www.someurl.com/custom_confirm/verify?email=test#test.com&hash=somehash
My hook menu in custom_confirm looks like this:
function custom_confirm_menu(){
$items = array();
$items['custom_confirm/verify'] = array(
'title' => 'Confirming Registration',
'page callback' => 'verify_email',
'access callback' => TRUE,
);
return $items;
}
My function
function verify_email()
Is not being invoked when I hit this URL. It's giving me a 404 and not hitting any code inside the verify_email function.
Is there something wrong with this hook_menu? I don't understand why this is not working?
Drupal 7 Hook_menu access callback is not returning the correct boolean.
Before we begin. YES! Cache is cleared... a lot.
I implemented a simple function for testing:
$items['tutor_review_selection'] = array(
'title' => t('example'),
'page callback' => 'my_module_example_page',
'access callback' => my_module_access( array('administrator') ),
'type' => MENU_NORMAL_ITEM
);
function my_module_access( $roles ) {
global $user;
$check = array_intersect($roles, array_values($user->roles));
return empty( $check ) ? FALSE : TRUE;
}
This returns TRUE for logged in and logged out users.
Here is the important part:
I call 'my_module_access' function in 'my_module_example_page' function and it works correctly.
Can anyone shine some light onto why this would not work in access callback?
Maybe something to do with order of operation?
Cache is cleared.
If you check the Drupal 7 hook_menu documentation you will see the following code:
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
);
return $items;
}
'page callback' accepts a string, which is the callback function name. The arguments to be sent to that function are provided in the 'page arguments' array.
edit Note that you should probably create a permission and assign your roles to that permission, then check the permission instead of checking for specific roles.
I waste all day fighting with drupal 7 hook_menu, days ago all was working when i create new modules, new menu entries, etc.
Im developing a cron, depends of 1 parameter, its generate a file to output, or read other file (input).
I trying to define a simple url to test the cron, and when i put ...lec_profile_cron in the brosers, it works, but if try ....companies_cron/1 or just companies_cron o other name y put at $items['route'], its doesnt works.
I tried to clean cache, memcached, use drush rr, all and dont understand what is happen.
I tried a lot of combinations and examples like the helloword_hello menu option in a new module i create called helloworld, and its returns 404 not found.
// CRON TEST
$items['companies_cron/%out'] = array(
'title' => t('Test cron'),
'page callback' => 'lec_profile_cron',
'page arguments' => array(1),
'access arguments' => array('administer lec profile configuration')
);
function lec_profile_cron($out)
{
// CRON OUT
if ($out == 1) {
//do stuff
} else {
//CRON IN
}
}
Maybe was a stupid thing, but i cant find...
Ty in advice.
According to the documentation I think this will work better. You dont need th %out for args in your $items and I also think you missing the trailing comma on 'access arguments' which I've added and also returning something.
$items['companies_cron'] = array(
'title' => t('Test cron'),
'page callback' => 'lec_profile_cron',
'page arguments' => array(1),
'access arguments' => array('administer lec profile configuration'),
return $items;
);
function lec_profile_cron($out = 0){
// If companies_cron/ is called, $out takes default value of 0
// If companies_cron/1 is called, $out value will be 1
// To pass multiple args, like companies_cron/1/2, you would require further params with defaults like $out = 0, $in = 0 ...
// CRON OUT
if ($out == 1) {
//do stuff
} else {
//CRON IN
}
};
I am using drupal 7.
I have a link which goes to href="/mod/filter/1"
X
and i have a a hook_menu
function mod_menu () {
$menu = array(
'mod/filter/%' => array (
"title" => "Bare HTML for use in ajax.",
"page callback" => "mod_remove_filter_function",
"page arguments" => array(1),
"type" => MENU_CALLBACK,
)
);
return $menu;
}
Then the callback function
function mod_remove_filter_function($arg){
dsm('call back filter');
drupal_goto('/res/search');
}
To me this should work, It is the first time i've used the menu hook but this looks like it should work according to the documentation given.
Any ideas why is doesn't work?
function mod_menu () {
$menu = array(
'mod/filter/%' => array (
"title" => "Bare HTML for use in ajax.",
"page callback" => "mod_remove_filter_function",
"page arguments" => array(1),
"type" => MENU_CALLBACK,
)
);
return $menu;
}
hook is workds perfect. problem might be in the callback function
dsm function requires devel module and if you are using the drupal_goto('/res/search'); check for the "/res/search" path first .
:)
here is how I use hook_menu in custom modules.
$menu['mod/filter/%'] = array(
'title'=>t('look this is title'),
'page callback' => 'mod_remove_filter_function',
'access callback' => 'user_access',
'access arguments' => array('access_contents'),
'type' => MENU_NORMAL_ITEM,
);
Do not use t() function in your menu item. By defualt drupal will pass the title string into the function t(). You can change that behavior by setting a new 'title callback' in your menu item array
See hook_menu book from drupal.org
made a simple thankyou page (e.g. /product/3/thankyou) based on a menu callback in a custom module. the content shows up fine in the normal page layout but i want the regions and blocks to show up and they don't. suggestions?
// menu callback
function custom_menu() {
$items = array();
$items['product/%/thankyou'] = array(
'page callback' => 'custom_product_thankyou',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
// theme function
function custom_theme() {
return array(
'product_review_thankyou' => array(
'variables' => array('node' => NULL),
'template' => 'product_review_thankyou',
),
);
}
// page callback
function custom_product_thankyou() {
$node = node_load(arg(1));
$output = theme('product_review_thankyou', array('node' => $node));
return $output;
}
I just tried your code in a drupal installation and i have no issues with missing blocks. Is it possible that you configured your blocks to be displayed only on certain pages?
The one block that i still couldn't get to show up (no matter what the block visibility settings were) was a 'menu block'. Problem was there wasn't a link for the thank you page in this block. So, I ended up having to add links on the configuration page with paths like product/[node_id]/thankyou, and then I disabled the links so they wouldn't be visible. Refreshed the page, and the block appeared.
To me this is a little annoying, as I kind of wanted this to be dynamic and not have to write in the product node ids. But either way, problem solved.