Drupal 7 menu error - drupal

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()

Related

Page Argument In Drupal

I have a block in that I have a search button, when clicks that button I pass this url (www.jksb.com/saleorderlist?field_month=4) to drupal menu. In page argument of menu I need to provide parameter (field_month).My page argument is like this: 'page argument' => array(1), but it doesn't work for me. I need somebody to help me out of this problem.
This is what I solve to this problem.
saleordermonthly.module
function sale_order_monthly_menu() {
$items = array();
$items["saleOrderMonthlyList"] = array(
'title' => 'Sale Order Monthly Report',
'description'=>'Sale Order Monthly',
'page callback' => 'sale_order_monthly_loadAllrecord',
//'page arguments' => array($para),
'access arguments' => array('access saleOrderMonthlyList'),
'type' => MENU_NORMAL_ITEM,
'file' => 'sale_order_monthly.admin.inc',
'access callback' => TRUE
);
return $items;
}
saleordermonthly.inc
enter code here
<?php
function sale_order_monthly_loadAllrecord()
{
$para='';
$query = drupal_get_query_parameters();
if(count($query) > 0){
$para= $query['field_month'];
};
}
?>
You are passing your argument in query string but if you use "page argument' => array(1)", it means you want the second segment/component of the path to be passed as the first parameter of your callback function. (you can still get the parameter inside your callback but you must use the "$_GET" variable in that case).
For instance :
$items['my-module/%/edit'] = array(
'page callback' => 'mymodule_abc_edit',
'page arguments' => array(1),
);
Here, "%" is a wildcard at position "1" ("my-module" is at position 0 and "edit" at position 2). This is the value that will be passed as the first parameter in the callback function ("mymodule_abc_edit")
In your case your path in "hook_menu" should look as follow:
'saleorderlist/%'
then if you call the path "saleorderlist/4", "4" will be passed as the first parameter of your callback function:
function my_callback($field_month) {
echo $field_month;//Will echo "4"
}
More info about hook_menu here ("Wildcards in Paths" section for your current question) : https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu/7

Valid access arguments

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

Drupal 7 example module, page not found, why?

I wrote a simple test module example, 2 files, test.module, test.info, and enabled them in drupal 7 modules.
I cleared all the cache, and still when i'm trying to go to localhost/drupal/hello , i get drupal 404 page not found, why is that?
Here is my code:
<?php
function test_world_help($section) {
switch ($section) {
case 'admin/help#hello_world':
$output = '<p>Hello world help...</p>';
return $output;
case 'admin/modules#description':
return 'Hello world module description...';
}
}
function test_world_menu($may_cache) {
$items = array();
if ($may_cache) {
}
else {
$items['hello'] = array(
'title' => 'Hello world page...',
'callback' => 'test_world_page',
'access' => TRUE,
'type' => MENU_CALLBACK
);
}
return $items;
}
function test_world_page() {
return '<p>Hello world!</p>';
}
You have posted almost the same question once and twice before. Why don't you update the original one instead of posting new ones?
The hook_menu() does not have the $may_cache argument in Drupal 7. You should remove it. However, it should not solve your problem as it is unset and false. Thus, the $items should still be populated.
It is correct, as jprofitt says, that you should change 'callback' to 'page callback'.
There is no such thing as 'access', but there is 'access callback' and 'access arguments'. You are most likely looking for 'access callback'. However, you can't just set it to 'true'. It expects a function name which returns either 'true' or 'false'. It defaults to 'user_access', so you should just leave it that way. However, you might want to set 'access arguments' to something like 'access content'.
Does the following piece of code work better?
function test_world_menu() {
$items = array();
$items['hello'] = array(
'title' => 'Hello World!',
'page callback' => 'test_world_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
It seems that you haven't really had a look at the documentation. I might be wrong. However, the documentation at api.drupal.org is always a good start to look when you want to learn the basics of how something work.
You should probably change 'callback' to 'page callback', as I don't believe hook_menu() has just a plain "callback" option. And since you're working with Drupal 7, its hook_menu() actually doesn't have parameters.
Uninstall and reinstall your custom module. I hope this will help you. Because it's necessary for drupal core to know the newly created path created using hook_menu.

drupal hook_menu_alter() for adding tabs

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.

Drupal problem, how to create a fast content module?

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

Resources