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
Related
How to display table result in webform on custom page? Only result page for custom role.
Create custom module and code in it :
create a hook menu with role authorisation :
function MYMODULENAME_menu(){
$items['my/custom/path'] = array(
'title' => t('Results'),
'page callback' => 'mycallback',
'access arguments' => array('role_name'), // 'access callback' => user_has_role('role_name'),
'type' => MENU_CALLBACK
);
return $items;
}
create callback :
function mycallback(){
// get your data from database or what you want
// your code ... $datas = [...]
// construct lines
$rows = array();
foreach($datas as $data){
$rows[] = array($data['title1'], $data['title2']);
}
return theme('table',
array(
'header' => array('Title 1','Title 2'),
'rows' => $rows
));
}
Annexes :
https://www.drupal.org/docs/7/creating-custom-modules
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7.x
Use views module:
https://www.drupal.org/project/views
When you are creating the view for "Show" option select "Webform submissions" instead of default "Content" value. Then you can tune your view as usual - create page view or block view, what ever you need.
And later, when you continue editing that view for "Format" option select "Table" (if it's not already set, as default value).
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()
how to display any message or data in mymodule.module file in drupal 7
i have used following line but it didn't display any thing
drupal_set_message(t('test message'));
aslo i want to display any variable data like for example $data = "hello"
then how to display this variable data in drupal 7
i am new to drupal , so if any one knows please let me know .
i have search a lot , but didn't get anything.
thanks in advance.
I have used folllowing code by creating module in drupal 7
<?php
function form_example_menu() {
$items = array();
$items['form_example/form'] = array(
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_example_form'),
'access arguments' => array('access content'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
function form_example_form($form, &$form_state) {
$form['price'] = array(
'#type' => 'textfield',
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
$form['form_example_form']['#submit'][] = 'form_example_form_submit';
return $form;
}
function form_example_form_validate(&$form, &$form_state) {
if (!($form_state['values']['price'] > 0)){
form_set_error('price', t('Price must be a positive number.'));
}
}
function form_example_form_submit($form, &$form_state) {
$result = db_insert('test')->fields(array('price' => $form_state['values']['price'],))->execute();
drupal_set_message(t('Your Price was saved'));
}
In above code data is inserted in database , but message didn't displaying .
If you know , what is problem please let me know , i have search a lot for this problem
. Thanks in advance.
Here's the proper way to display some data in the message:
drupal_set_message(t('test message: !data', array('!data' => $data)));
As for the message not displaying, if other messages do display on your site, it sounds like your function isn't executing. I'd need more info on what you're attempting to do (including the code involved) to debug it.
The function watchdog is also available in Drupal 7
Here is an exemple of how you can use it:
watchdog('MyModule', '<pre>'. print_r($variable, TRUE) .'</pre>', array(), WATCHDOG_INFO, NULL);
You can watch the log in Reports -> Recent log messages (admin/reports/dblog) if the core module "Database logging" is activated.
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.