Drupal 7 Access Callback Not Working Properly - drupal-7

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.

Related

hook_permission and control access [how to redirect to the login page if user not logged in]

How does one limit "what" can access a page. I have little understanding on how to use hook_permission() to set user permission based on a role to control access to hook_menu() items. However, what if I have a hook_menu() item that is to be used solely by the system itself? For example, say I have a registration page in a custom module: module/register - Anyone can access that page. Then, say I have an another page which is only for admin. So, I register another item in hook_menu(), call it module/register/reg_user_details - I do not want anyone to be able to browse to module/register/reg_user_details
Instead I want page to be visible only when user is logged in, I have already created a hook_permission
/**
* Implements hook_menu()
*
*/
function video_subtitles_menu() {
$items = array();
$items['video_subtitles/upload'] = array( //this creates a URL that will call this form at "video_subtitles_test/upload"
'title' => 'Upload Subtitle', //page title
'description' => 'Uploading subtitle for videos',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, drupal_get_form need to be used
'page arguments' => array('video_upload_subtitles_form'), //Name of the Uplaod Form
'access callback' => 'user_access',
'access arguments' => array('administer video_subtitles module'),
);
$items['player/video_subtitle_status'] = array(
'page callback' => 'video_subtitle_status',
'access callback' => 'user_access',
'access arguments' => array('administer video_subtitles status'),
);
return $items;
}
/**
* Implements hook_permission.
*/
function video_subtitles_permission() {
return array(
'administer video_subtitles module' => array(
'title' => t('Administer video_subtitles module'),
'description' => t('Access the video_subtitles upload module Page'),
),
'administer video_subtitles status' => array(
'title' => t('Administer video_subtitles module status'),
'description' => t('Access the video_subtitles module status Page'),
));
}
So User is not able to access my page unless user is logged in, but
what I need user should be redirected to the login page when he is not logged in as admin , if he successfully login it should be redirected to my page.
visit url d-7/example/my-module
if already logged in show the page
if not logged in redirect to the login page
if user logged in successfully redirect to d-7/example/my-module page
What is best way to achieve this
Similar Question
using-hook-menu-with-hook-permission-access-denied
using-hook-menu-and-hook-permission-to-control-access
can-someone-explain-access-arguments-in-drupal

hook_menu not working for me, neither helloworld example

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
}
};

drupal menu callback hook not calling function

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

drupal 7 access callback function

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.

How do I make certain menu items visible to certain roles?

In /drupal/admin/structure/menu/manage/main-menu, I have some links that I only want displayed for user's that have a specific permission.
How would I go about this?
In my module, I have
...
$items['resume/joblist'] = array(
'page callback' => 'ac_resume_job_list',
'access arguments' => array('view joblist'),
'title' => 'Job List',
'description' => 'Job List',
);
...
function ac_resume_permission()
{
return array("view joblist" => array("title" => "View Job List"));
}
When I go to "resume/joblist" under a user without the permission, I get the "Access Denied" as expected, however the link is still displayed.
hook_perm() was renamed to hook_permission() in Drupal 7, and there's a bit of a mismatch between view mylink that you define and view joblist that you declare as a permission.
You could change your code to look more like this:
function mymodule_menu() {
$items['mylink'] = array(
'page callback' => 'mymodule_mylink',
'access arguments' => array('view mylink'),
'title' => 'My Link',
'description' => 'My Link',
);
return $items;
}
function mymodule_permission() {
return array(
'view mylink' => array(
'Title' => 'View My Link'
)
);
}
After you clear Drupal's cache navigate to admin/people/permissions and assign your new permission to the role you want to be able to access the page you define in hook_menu().
Once you've done that users with that role will be able to access the page :)
I recommend using the following module: Menu Item Visibility, it does exactly what you need.

Resources