Pass wrong values by menu items when using drupal_get_form - php

I want to send a parameter to my function with drupal_get_form() in the menu item, but not works and wrong values send i don't have any idea about this error.
First i call the menu item from this line in onther file:
drupal_goto( 'a/b/c/d/'.$form_state['values']['number']);
here is my menu item code in a different file:
$items['a/b/c/d/%'] = array(
'title' =>t('report'),
'description' => 'my report',
'page callback' => 'drupal_get_form',
'page arguments' => array('make_report',1),
'access callback' => 'user_access',
'access arguments' => array('admin'),
'file' => 'report_file.inc',
);
and this is my function header in report_file.inc:
function make_report(&$form_state,$back=0){
.
.
this is the variable dump of $form_state:
storage (NULL)
submitted (Boolean) FALSE
post (Array, 0 elements)
this is the variable dump of $back:
b
i want the $back pass '1' but it get the second level of my url.

Related

Drupal - redirect url custom module

I a stupid problem but nothing is working.
Lets say i have a url : something/4
Then i use a function users_view to display output.
Menu for users_view
$items['users/%'] = array(
'title' => 'your info',
'page arguments' => array(1),
'page callback' => 'users_view',
'access arguments' => array('view user'),
'type' => MENU_NORMAL_ITEM,
);
Now i wont to get to it ...
I used another function to get to it via a link in main menu. I created a function user_created that gives me the user_id(this is just an example)
function users_menu (){
$items=array();
$items['users/'] = array(
'title' => 'Your info',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array ('view user'),
'page callback' => 'user_created',
);
return $items;
}
function user_created(){
global $user;
$itemid=array();
$temp_user= $user->uid;
drupal_set_title('Your data');
$result = db_query("SELECT user_id FROM {user_test}
WHERE current_user=:s", array(':s' => $temp_user));
foreach($result as $item) {
$itemid = $item->user_id;
}
REDIRECT to url users/$itemid
}
I have used drupal drupal_goto , $form['redirect'] but nothing happens ...
The problem is it just says users/
You have an error, you have to fetch the result like this :
$result = db_query("SELECT user_id FROM {user_test}
WHERE current_user=:s", array(':s' => $temp_user));
$itemid = $result->fetchField();
drupal_goto('user/'.$itemid);

How can I load a Drupal Form via ajax into a jQuery dialog?

I have a module in which I build a form. I can hit form via a menu item that was also created in that module. When I try to load the form via ajax I get the entire page (header, form and footer) instead of just the form. Here's the menu item:
$items['sendmessage'] = array(
'title' => 'Send Message',
'description' => 'Send a message',
'page callback' => 'drupal_get_form',
'page arguments' => array('rmessages_message_form', 1),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
);
Here's the javascript:
$('.send_message').click(function(){
$('.send-message-dialog').dialog('open');
$('.send-message-dialog .dialog-content').load('/sendmessage/7');
}
);
If I load the URL via a browser, the form loads. Is there some way to get drupal to just render the form and return the HTML rather than trying to load the entire page.
This seems to be working...gotta remember the drupal_render function ;)
$items['sendmessage'] = array(
'page callback' => 'rmessage_send_message_form',
'page arguments' => array('rmessages_message_form', 1),
'access callback' => TRUE
);
Using drupal_build_form to get an array of items, which gets rendered by drupal_render()
function rmessage_send_message_form($form_id, $nid) {
$form_state = array();
echo drupal_render(drupal_build_form('rmessages_message_form', $form_state, $nid));
}
I think the nicest way to do it would be to have your form accessible from the normal URL (for those without JavaScript) and available to AJAX. You can do that like this:
function rmessage_menu() {
$items['sendmessage/%node'] = array( // Using the '%node' load argument ensures that the nid attempting to be accessed belongs to an existing node.
'title' => 'Send Message',
'description' => 'Send a message',
'page callback' => 'rmessages_message_form',
'page arguments' => array(1),
'access callback' => TRUE
);
return $items;
}
function rmessages_message_form($node) {
$form = drupal_get_form('rmessage_send_message_form', $node->nid);
// Just print the form directly if this is an AJAX request
if (isset($_GET['ajax'])) {
echo render($form);
// Halt page processing
drupal_exit();
}
// Otherwise return the form as normal
return $form;
}
Then in your JS you would just need to add the query string:
$('.send_message').click(function(){
$('.send-message-dialog').dialog('open');
$('.send-message-dialog .dialog-content').load('/sendmessage/7?ajax');
}
);
Hope that helps

Drupal get parameters from url in edit form and populate the form

i have an employee table with fields like id, name, age and salary.
Iam showing the list of employee names in my custom module and when i click on an employee name, i have to show the edit form of that employee.
The employee names are given a link, as like:
<a href='".$base_url."/my_module/edit/".$employee->id."'>
and the corresponding menu path is configured as:
$items['my_module/edit/%'] = array(
'title' => t('My form'),
'description' => t('Edit employee'),
'page callback' => 'my_module_edit',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
And when i click on the 5th employee name, the url would be: ..../my_module/edit/5
So now my questions are:
How can i get the id (5) of the employee from the url in my my_module_edit function ?
How can i show the edit form of the employee, populating the fields (by querying with id) ?
For question 2: You have got it mixed up a bit, you don't create a custom 'page callback' menu item for forms, you should use 'drupal_get_form' as the 'page callback' with the name of your form() function (my_module_employee_form) as the 'page arguments'.
function my_module_menu() {
$items['my_module/employee/edit/%'] = array(
'title' => t('Edit employee'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_employee_form', 3),
'access arguments' => array('administer my_module'),
'type' => MENU_CALLBACK,
);
}
function my_module_employee_form() {
... read database and generate form..
}
function my_module_employee_form_submit($form_id, &$form) {
... read form and update database ...
}
Then in the function my_module_employee_form() you generate the form and populate it, and in my_module_employee_form_submit() you read the values back and store them in the database.
$request_parts = explode('/', $_SERVER['REQUEST_URI');
$employee_id = intval(array_pop($request_parts));
That's for your question #1. Not sure I understand #2
For your first question, you can get the arguments passed to a callback by using arg(0), arg(1), arg(2) etc...
<a href='".$base_url."/my_module/edit/".$employee->id."'>
For this href arg(0) == 'my_module', arg(1) == 'edit', arg(2) == '5', take a look at arg()'s documentation.
You can also parse the query string your self by accessing the $_GET['q'] variable.
For your second question, if your employee table is a Drupal table you can just make the link be the edit link to the Drupal node, like href="node/5/edit". If it is a custom table you will have to create your own form by implementing hook_form()

Drupal drupal_get_form

I trying to get the twitter_admin_form and twitter_user_settings form in a div.
/**
* Get twitter form for user
* #param $account
* #type user object
*/
function getTwitterForm($account){
//module_load_include('inc', 'twitter');
module_load_all();
$twitter_form = drupal_get_form('twitter_admin_form');
return $twitter_form;
}
I get a get a drupal error.
warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'twitter_admin_form' was given in .../includes/form.inc on line 372.
twitter.module
/**
* Implementation of hook_meu()
*/
function twitter_menu() {
$items = array();
$items['admin/settings/twitter'] = array(
'title' => 'Twitter setup',
'description' => 'Twitter module settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('twitter_admin_form'),
'access arguments' => array('administer site configuration'),
'file' => 'twitter.pages.inc'
);
$items['user/%user_category/edit/twitter'] = array(
'title' => 'Twitter accounts',
'page callback' => 'twitter_user_settings',
'page arguments' => array(1),
'access arguments' => array('add twitter accounts'),
'load arguments' => array('%map', '%index'),
'weight' => 10,
'file' => 'twitter.pages.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
I'm not sure what I'm doing wrong. The twitter_admin_form doesn’t have any arguments hence I thought it would be simple to get and display.
I’m new forms/menu so I’m not 100% sure what %user_category, %map and %index are and how to pass them in.
How do you know what the valid forms are?
When you call drupal_get_form you supply a form id, which is the function that Drupal needs to call. The problem you are experiencing is that Drupal cannot find the function: twitter_admin_form.
Either it's located in an include file, and you need to include it, or you have named it something else.
The error you get stems from the line:
$twitter_form = drupal_get_form('twitter_admin_form');
It expects 'twitter_admin_form' to be a valid callback function, but can't find it. This is probably because the related file 'twitter.pages.inc' is not included at the time of your call.
You could fix that via a:
module_load_include('inc', 'twitter', 'twitter.pages');
(Given the commented line in your code sample, you seem to have tried something like this, but forgot to give the name of the file to include).

Drupal Module Development hook_menu() For Semi Static Pages

I have a page that is definitely not a form but I need to use some callback functions to load data from an external source and display (e.g. a list of buildings on campus and their accessibility information).
What I have a need for is a landing listing page (lists all the buildings) and a 'view individual building' page. Also, I have a page where you punch in your student ID and view information on testing procedures. And finally I have a page that is basically a form (which I have done before successfully in the past).
Now, I HAD the building list working, however I made a small change and it stopped working!
Currently my hook_menu() function looks as below:
<?php
/**
* Implementation of hook_menu()
*/
function disability_menu()
{
$items = array();
// Ignore me, shell
$items['quickreg'] = array(
'title' => 'Quick Registration',
'description' => t(''),
'page callback' => 'drupal_get_form',
'page arguments' => array(),
'file' => 'disability.quickreg.view.inc',
'access arguments' => array('access quick registration system'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['tests/status'] = array(
'title' => 'Test Status Results',
'description' => t('Check on the status of your tests'),
'page callback' => 'disability_view_testing_status',
'page arguments' => array(),
'file' => 'disability.tests.view.inc',
'access arguments' => array('access test check information'),
'type' => MENU_CALLBACK,
);
$items['tests'] = array(
'title' => 'Testing Services',
'description' => t('Check on the status of your tests'),
'page callback' => 'disability_view_testing',
'page arguments' => array(),
'file' => 'disability.tests.view.inc',
'access arguments' => array('access test check information'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['access/%building'] = array(
'title' => 'Campus Accessibility Guide',
'description' => t('A summary list of detailed accessibliity information about each building on the A&M campus'),
'page callback' => 'disability_view_access',
'page arguments' => array(1),
'file' => 'disability.access.view.inc',
'access arguments' => array('access building access information'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
Before some change I must have made the menu item for "Campus Accessibility Guide" would show up properly (after being enabled of course). The /access url would work correctly displaying a list of all building and the /access/12345 would correctly display the single record of ID# 12345.
Now the access/%building menu entry is not even showing up and even sending the url /access into a redirect loop (making me think it's passing in something for the ID which sends it into the view specific function that redirects to /access when the ID doesn't exist).
Can anyone tell me what I'm doing wrong or what I need to do to support 2 themed pages: a /access and /access/%building url pattern?
You should only use %name instead of % in urls when you have a function that you want to act on the url. Drupal does this all over the place with user and node, and this is very smart, as you only one place need to have the code to load an user or a node, but it get used in a lot of places. In this case I bet it's a bit overkill to make a function to load the building. On the other hand the advantage is that doing it that way, you get the 404 handling, if no object can be found. The best solution really comes down to how you want to handle buildings that does not exist. You could even make your 'Campus Accessibility Guide' function handle the 404 which would make the two options more or less equal. I would go for whatever is easiest for you to make.

Categories