How can I look up the valid access arguments? I looked in menu_router, but I believe that only gives some of them.
$items['admin/page'] = array(
'access arguments' => array('access administration pages'),
);
Invoke hook_permission() across all modules:
$permissions = module_invoke_all('permission');
If I remember rightly array_keys($permissions) will then give you a list of valid permission machine names. The labels/descriptions/other settings for each permissions are in each individual array item.
Actually, you are interested to the values of the access arguments where the access callback is "user_access" (the default value); as a module can use a different access callback, the values for the access arguments can theoretically be infinite.
The alternative to invoking all the implementations of hook_permission() is to use code similar to the following one:
$permissions = array();
db_query("SELECT permission FROM {role_permission}");
foreach ($result as $row) {
$permissions[$row->permission] = TRUE;
}
array_keys($permissions) will then give you the list of all the permissions.
I took the query from user_role_permissions(); the difference is that the function is interested in the permissions associated to the role passed as argument.
1- Check a list of valid permissions at: /admin/people/permissions
2- Specify the permission in your menu hook:
function webforms_advanced_router_menu() {
$items['admin/config/mymodule'] = [
'title' => 'MyModule',
'page callback' => 'drupal_get_form',
'access callback' => '_mymodule_admin_form',
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK
];
return $items;
}
Related
I writing custom module for Drupal 7 and got the following warning:
Warning: Invalid argument supplied for foreach() в функции menu_unserialize() (строка 400 в файле /var/www/auth/includes/menu.inc).
My hook_menu is here:
function mnogomirauth2_menu() {
$menu['tables/udkservers'] = array(
'title' => 'udkserversTable',
'page callback' => '_menu_test',
'access arguments' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $menu;
}
function _menu_test()
{
echo "test";
}
Please, tell me, what's wrong with this code?
Best regards.
According to Drupal API for hook_menu
"access arguments": An array of arguments to pass to the access
callback function, with path component substitution as described
above. If the access callback is inherited (see above), the access
arguments will be inherited with it, unless overridden in the child
menu item.
It seems that you have specified wrong argument TRUE for access arguments.
The below mentioned code will solve it:
Before:
'access arguments' => TRUE,
After:
'access arguments' => array('Your Permission'), //array(TRUE)
The code that work is the following one.
function mnogomirauth2_menu() {
$menu['tables/udkservers'] = array(
'title' => 'udkserversTable',
'page callback' => '_menu_test',
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM
);
return $menu;
}
It's the access callback that can be a number. If it evaluates to TRUE, every user has access to the menu item; it it evaluates to FALSE, no user has access to the menu item.
References
The documentation for hook_menu()
I am currently using Drupal 7 and I am writing a custom code such that users with a certain permission("use business dashboard") should see a menu item in their main menu. The problem is that only I(admin) can see this menu item. I have been able to create a custom permission on the permissions page and have set it to give access to "admin" and my user-specific role and have implemented the following code(nevermind the "xxxxxx" that is inplace of the module name, I would rather keep it anonymous for now, but just know that they are all in place of the machine-readable module name):
function xxxxxx_menu(){
$items = array();
$items['xxxxxxx'] = array(
'title' => 'Business Owner Dashboard',
'page callback' => '_xxxxxx_page',
'access arguments' => array('use business dashboard'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function xxxxxx_permission(){
return array(
'use business dashboard' => array(
'title' => t('Have access to business dashboard'),
'description' => t('Allow user to send out SMS messages via database query forms'),
),
);
}
When I log in as my test user which has the role-specific permission of "use business dashboard" I cannot see the menu item. I am sure this is incredibly simple, but I have been Googling and prodding at the code for hours. Any help would be greatly appreciated!
Can't figure this out either. Can you try to break down the access callback, if it didn't work, at least it'll give you a tip about what's going on.
Your code can go like this:
function xxxxxx_menu(){
$items = array();
$items['xxxxxxx'] = array(
'title' => 'Business Owner Dashboard',
'page callback' => '_xxxxxx_page',
'access callback' => 'my_custom_access_callback',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_custom_access_callback()
{
if(user_access('use business dashboard'))
return TRUE;
return FALSE;
}
Till me if this works... Muhammad.
I have this form which accepts user’s input. What I like to do is, base on that user input, I’d like to retrieve data and display it back to user.
So far, I have implemented hook_menu and registered respective url of the form, and implemented a submit function referred by “#submit” attribute of submit button. I’ve also implemented data retrieval code and works great.
Here’s my problem – I don’t know how to display retrieved data. I’ve tried several approaches in an attempt to find the solution.
First, with theme function, hoping that printing the return value of it would display the data. Second, setting “#action” element of form array with newly registered url, as I thought using the same url as form would only cause drupal to return that form instead and not my data. So, I creates a static variable and stores all the retrieved data in it;this is done inside submit function by the way. When I checked this variable inside menu callback, this variable is not set.
To summarize my problem, form has different access url than form submit, such as
Form url – http://....?q=mymodule/form
Submit url (value of ”#action”) – http://....?q=mymodule/execute
, and the data I’ve set inside submit function to static variable is not available in menu callback. How do I make the data available?
Here’s part of my code -
static $retrieved_data;
function mymodule_menu() {
$command = array();
$command['mymodule/form'] = array(
'title' => 'user input',
'page callback' => 'response',
'page arguments' => array('form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$command['mymodule/execute'] = array(
'title' => 'Search',
'page callback' => 'response',
'page arguments' => array('execute'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $command;
}
function _response($paRequest){
switch($paRequest){
case "form":
return drupal_get_form("_myform");
break;
case "execute":
return $retrieved_data;
break;
}
}
function _myform(&$form_state) {
$form['#action'] = url($base_path)."?mymodule/execute";
.....
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#submit' => array('_data_retrieve'),
);
return $form;
}
function _data_retrieve($form, &$form_state){
/*data retrieval code*/
........................
$retrieved_data = db_fetch_object($result);
}
Thanks a bunch
Your method seems a bit complicated there. When I make systems with a form, I tend to do it this way. In your MYMODULE_menu() I would change the 'page arguments' => array('form'), to 'page arguments' => array('NAME_OF_FORM_FUNCTION'), where NAME_OF_FORM_FUNCTION would be _myform in this case. I would rename it to MYMODULE_MYFORMNAME.
then create a function:
MYMODULE_MYFORMNAME_SUBMIT($form, &$state) {
// Enter code here to save the data from the form that is stored in $state
// to the database with an SQL query or a node_save($node) if you are
// creating a node.
}
After that you can retrieve the data from the database in your _data_retrieve function and call that on the page where you want to retrieve the data.
I want to add some tabs in the "node/%/edit" page from my module called "cssswitch".
When I click "Rebuild Menus", the two new tabs are displayed, but they are displayed for ALL nodes when editing them, not just for the node "cssswitch". I want these new tabs to be displayed only when editing node of type "cssswitch".
The other problem is when I clear all cache, the tabs completely dissapear from all edit pages. Below is the code I wrote.
function cssswitch_menu_alter(&$items) {
$node = menu_get_object();
//print_r($node);
//echo $node->type; //exit();
if ($node->type == 'cssswitch') {
$items['node/%/edit/schedulenew'] = array(
'title' => 'Schedule1',
'access callback'=>'user_access',
'access arguments'=>array('view cssswitch'),
'page callback' => 'cssswitch_schedule',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight'=>4,
);
$items['node/%/edit/schedulenew2'] = array(
'title' => 'Schedule2',
'access callback'=>'user_access',
'access arguments'=>array('view cssswitch'),
'page callback' => 'cssswitch_test2',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight'=>3,
);
}
}
function cssswitch_test(){
return 'test';
}
function cssswitch_test2(){
return 'test2';
}
Thanks for any help.
hook_menu_alter() is only called during the menu building process, so you can't do dynamic node type checks within that function.
However, to achieve what you want, you can do this with a custom access callback as follows:
// Note, I replaced the '%' in your original code with '%node'. See hook_menu() for details on this.
$items['node/%node/edit/schedulenew2'] = array(
...
'access callback'=>'cssswitch_schedulenew_access',
// This passes in the $node object as the argument.
'access arguments'=>array(1),
...
);
Then, in your new custom access callback:
function cssswitch_schedulenew_access($node) {
// Check that node is the proper type, and that the user has the proper permission.
return $node->type == 'cssswitch' && user_access('view cssswitch');
}
For other node types, this function will return false, thus denying access, and thus removing the tab.
Hi I need to create a module in drupal to display some data, not being a drupal developer and after reading a couple of tutorials, i cant seem to display anything.
I have the next code:
<?php
function helloworld_perm() {
return array('access helloworld content');
}
function helloworld_listado(){
return "yea";
}
function helloworld_menu(){
$items = array();
$items["listado"] = array(
'title' => t('Listado de empresas'),
'callback' => 'helloworld_listado',
'access' => array('access helloworld content'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
When i enter /listado i get a Access denied
You are not authorized to access this page.
Any idea what im doing wrong?
If i go to the admin->module->permissions, i have checked the permision for all roles to access hellowold content.
Ty!
From the way that your menu array is structured in helloworld_menu(), I'm assuming that this is Drupal 6. If so, you need to rename 'access' to 'access arguments'. See http://api.drupal.org/api/function/hook_menu/6.
The Drupal API documentation also includes a heavily-commented page_example.module that's doing basically what you're doing here, which you might want to check out: http://api.drupal.org/api/file/developer/examples/page_example.module/6/source
Hope that helps!
Oh. And don't forget to clear your cache afterwards from the "Clear cache" button on Administer >> Site configuration >> Performance.
=> t('Listado de empresas'),
'page callback' => 'helloworld_listado',
'access arguments' => array('access helloworld content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
Note that , MENU_NORMAL_ITEM being the default value for type, you do not need to specify it.
Also, as our esteemed webchick just said
It seems you are using a mix of Drupal 5 (array contents) and Drupal 6 (no $may_cache, $items indexed by path) syntaxes for hook_menu.
If you are using Drupal 6, this should look like:
<?php
function helloworld_perm() {
return array('access helloworld content');
}
function helloworld_listado(){
return "yea";
}
function helloworld_menu(){
$items = array();
$items["listado"] = array(
'title' => t('Listado de empresas'),
'page callback' => 'helloworld_listado',
'access arguments' => array('access helloworld content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
?>
Note that, MENU_NORMAL_ITEM being the default value for 'type', you do not need to specify it.
Also, as our esteemed webchick just said, you can find a detailed explanation on the page she points to.
just FYI , above link moved to
http://api.drupal.org/api/examples/page_example--page_example.module/6