How do i get the data?(Drupal 6.x) - drupal

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.

Related

Passing a variable from a form to another in Drupal

Let's say I have custom module with Drupal.
I want to let user to type what skill their have in first form and after that show their skill as a title in second form and type percentage of their skill in textfield.
function skillbar_form($form, &$form_state) {
$form['html5'] = array(
'#type' => 'textfield',
'#title' => t('HTML5'),
'#default_value' => variable_get('html5'),
'#description' => t('Enter a percent of your HTML5 skill'),
);
return(system_settings_form($form));
}
Multistep Form is you friend, if you are building your form using the Form API.
Otherwise there are various modules which could help you in building forms with more than one step. A couple of them being:
Multi-step forms
Multi-Step Registration
Note: Multistep forms generally collect all the data from various steps. The data is mostly submitted at the final step (which is also a best practice).
You can pass the data from one form to another through URL in the submit form of first form.
function skillbar_form_submit($form, &$form_state){
$data_a = $form_state['values']['html5'];
$form_state['redirect'] = array('url_page2', $data_a);
}
In the other form just retrieve the data as by passing the $data_a as argument. So $var will have the value as 'html5'.
example_form($form ,&$form_state, $var){
...
}
And for the URL you will have to send it in items array
$items['url_page2/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('page2_form', 1),
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);

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

How to hook_load a CCK custom content type in a MENU_CALLBACK drupal

This is for the DRUPAL ninjas:
I am creating a menu_hook for my custom module called testmodule;
I want to call a MENU_CALLBACK and return a loaded CCK object of a custom content type I created called "VideoNodes"
example:
$items['save_video_data/%my_custom_cck_type/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'save_data_to_db',
'access arguments' => array('Save Data to my_custom_cck_type'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'title' => 'Save a Data!'
);
Now, with the above code, my understanding is that I am telling drupal to load my_custom_cck_type and use the array(2) position (my argument) as the id field of the node to find it in the database.
I also understand that, I am going to need to create a my_custom_cck_type_load() function.
QUESTION:
What shoudl be in my_custom_cck_type_load()?? How shall I grab all of the custom fields associated with the CCK type into the returned object? Or does drupal do this for me?
I'll use node_load as an example for this. Out of the box Drupal's menu system will load nodes at paths like.
node/1/view
node/2/view
node/3/view
(note: the "/view" part of the path is often hidden because it's the default tab)
The menu item setup to do this looks something like:
$items['node/%node/view'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'blabla',
'page callback' => 'node_view',
'page arguments' => array(1)
/* more stuff */
);
In the real path the %node is replaced by a number, the NID of the node. This will cause menu to fire node_load using that number as the argument. We're also using %node as the page argument for the node_view callback. The argument that callback receives will be the fully loaded node object.

hook_form_submit not being called

I'm trying to submit a form and use hook_form_submit.
The problem is the form is displayed via ajax and this results in hook_form_submit not being called.
$items['ajaxgetform/%'] = array(
'page callback' => 'ajaxgetform',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
function ajaxgetform($form_id) {
drupal_get_form($form_id);
return drupal_json($panel);
}
function_myform_form($form_state) {
$form['myform'] = array(
'#title' => 'myform value',
'#type' => 'textfield',
'#default_value' => 'myform default value'
);
$form['#action'] = url('myurl');
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'submit'
);
$form['#ajaxsubmit'] = TRUE;
return $form;
}
hook_form_alter() does get called.
Below doesn't get called?
function myform_form_submit($form, $form_state) {
// ...
}
I'm not sure if this is a common problem, but i've been stuck for hours trying to make it work.
If I remove $form['#action'] = url('myurl'); myform_form_submit() gets called. However I get a white screen with jason script.
There is no hook_form_submit(). Instead, you register submit handlers with $form['#submit']. So, if you want to call myform_form_submit() when the form gets submitted, add:
$form['#submit'][] = 'myform_form_submit';
to myform_form(). Take a look at the 5.x to 6.x form changes and the Forms API reference for more info.
Is your form displayed on the page at myurl ? In order for a form submission to be processed, the form as to be displayed (using drupal_get_form()) on the page used as action.
You may also try to se the form #redirect to the landing page URL instead of its #action. This way, the form is submitted to its generating URL but the user is redirected to your destination page after processing.

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.

Resources