Drupal:Direct a user to a specific page after submitting - drupal

I have a submitt drupal form in a my module:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
And this is my submitt function , what command have to add to direct the user to a specific page with a specific path? I tried this but it didnt work
function testform_submit($form, &$form_state) {
$form_state['submit'] = 'https://www.google.de/';
}

So close...
function testform_submit($form, &$form_state) {
$form_state['redirect'] = 'https://www.google.de/';
}

Related

Check if form failed validation

I added a validation hook to a node creation form.
For my project, i need this validation only ONE time: basically i just check if a value already exists in the DB and i notify it to the user. If the user still submits the form, i must add it anyway.
i would like to do something like that:
if (form_did_not_fail_validation_before) {
$form['#validate'][] = 'my_module_validation_hook';
}
in my hook_form_alter, so that validation hook is only called the first time.
I can't find a way to check if the form already failed validation, any ideas?
Here is one way of solving this issue your issue. But if you using some form input like text form I suggest that you use Ajax callback since it will feel more dynamic. Let me know if you want an example of how to solve it with Ajax callback?
I guess the key here is that I use $form_state['rebuild'] = true; so that form is rebuilt and than it easy to get the value from the $form_state['values']
Here is the code:
function example_form(array $form, array &$form_state)
{
$form['temp'] = array(
'#type' => 'value',
'#value' => array_key_exists('values', $form_state) ? $form_state['values']['temp'] + 1 : 1,
);
if (array_key_exists('values', $form_state) && $form_state['values']['temp'] == 1)
{
$form['message'] = array(
'#markup' => t('I only give you one warning'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
}
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
return $form;
}
function example_form_validate(array $form, array &$form_state)
{
$form_state['rebuild'] = true;
}
function example_form_submit(array $form, array &$form_state)
{
if($form_state['values']['temp'] == 1) {
// I will not store anything
} else {
// now I will store it :)
}
}

drupal ajax form change

I'm working on a drupal 7 module, where I wish to print out infos on a page (MENU_LOCAL_TASK node/%node/something), with ajax filters.
I created a form and added 2 checkboxes, 1 is on default other is not. I want to show to the user the information according to wich checkbox is checked. 1 is on table row 1 is displayed, 2 is on table row 2 is displayed. If some of it is off, than that table row is off. Did I mentioned, that I want to solve it without submit and reload, only ajax.
I added to the two 'checkbox'es the following 'ajax' => array('callback' => 'my_module_callback')
. Here is the rest of the code, simplefied.
function my_module_callback($form, $form_state) {
$data = array();
$nid = 1;
if ($form_state['values']['checkbox1']) {
$data += load_data($nid, "checkbox1");
}
if ($form_state['values']['checkbox1']) {
$data += load_data($nid, "checkbox2");
}
$commands[] = ajax_command_html("#here", my_module_table($data));
return array('#type' => 'ajax', '#commands' => $commands);
}
function my_module_table($data){
//do some stuff with the data in a foreach
return theme("my_module_fancy_table",array("data" => $data));
}
function theme_my_module_fancy_table($data){ //registered with my_module_theme()
// putting html into $output in a foreach
return $output;
}
function my_module_page_callback_from_menu_function($nid){
$output = drupal_render(drupal_get_form('my_module_custom_ajax_form'));
$output .= "adding other stuffs including div#here";
return $output;
}
First of all is this the 'good way' to do this, cause I kind of lost confident:)
Second question, how to show the data on page load, rigth now one checkbox needs to be changed to see some infos.
Thanks and sorry for the short description :)
You should not really be doing the processing in the callback, it should be done in the form building function. The callback usually only returns the part of the form that has changed. Also, I don't think there is not need for setting commands[] in this case as returning part of the form will automatically replace the content set by 'wrapper'.
function my_module_form($form, $form_state){
$data = array();
$nid = 1;
if ($form_state['values']['checkbox1']) {
$data += load_data($nid, "checkbox1");
}
if ($form_state['values']['checkbox2']) {
$data += load_data($nid, "checkbox2");
}
$form = array();
$form['checkbox1'] = array(
'#type' => 'checkbox',
'#ajax' => array(
'callback' => 'my_module_callback'
'wrapper' => 'mydata',
'event' => 'change',
),
);
$form['checkbox2'] = array(
'#type' => 'checkbox',
'#ajax' => array(
'callback' => 'my_module_callback'
'wrapper' => 'mydata',
'event' => 'change',
),
);
$form['mydata'] = array(
'#prefix' => '<div id="mydata">',
'#suffix' => '</div>',
'#markup' => my_module_table($data),
);
return $form;
}
function my_module_callback($form, $form_state){
// $form_state['rebuild'] = true; may have to be set because the form has not been submitted and wont be rebuilt...I think, I cant remember for sure.
return $form['mydata'];
}
To show the data on page load, you just have to change the logic of setting data in the form build function.
Also, fyi, there is a stack site specifically for drupal at: http://drupal.stackexchange.com

file association with large amount of users

I am sorry if this is a dumb question I have the following code
function trc_upload_file() {
$form['trc_upload_form'] = array(
'#type' => 'managed_file',
'#title' => 'Upload File',
'#descripion' => 'Uplaod files',
);
$form['#submit'][] = 'trc_upload_file_submit';
return $form;
}
function trc_upload_file_submit($form, &$form_state) {
$file = file_load($form_state['values']['trc_upload_form']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);
/*file_usage_add($file, 'trc_upload_page', 'user', $account->uid);*/
drupal_set_message(t('You Uploaded Successfully!'));
}
this is working fine
how to use file_usage_add() function, can i use it for user_roles instead individual users.
As Clive has confirmed, this should work:
file_usage_add($file, 'trc_upload_page', 'role', $roleID);
Let us know how you get on,

Ask twice for email in the Ubercart checkout page

Is there a Ubercart module to ask the user to insert his email twice in the checkout page?
There is an email confirmation checkbox in ubercart checkout settings. No additional modules needed.
I doubt there is a module for this. You can do this with hook_form_alter in a custom module. Should only be 10-20 lines of code.
Something like
function module_form_FORM_ID_alter(&$form, &$form_state) {
$form['...']['second_mail'] = array(
'#title' => t('Verify E-mail'),
'#type' => 'textfield',
'#weight' => xx,
);
$form['#validate'][] = 'module_validate_function_name';
}
function module_validate_function_name(&$form, &$form_state) {
if ($form_state['values']['mail'] != $form_state['values']['second_mail']) {
form_set_error('second_mail', t('You have mistyped your e-mail, please verify');
}
}
The above is example code, but might actually work, it depends how the ubercart checkout form is created, more specifically, the name of it's mail field.
There are a few blanks but it should be easy enough to fill out.
I got it working by using this:
/* Code to add confirm email for uc checkout */
function custom_code_form_alter(&$form, $form_state, $form_id) {
if($form_id == "uc_cart_checkout_form" && $form['panes']['customer']['primary_email']['#type'] != 'hidden'){
$form['panes']['customer']['primary_email']['#weight'] = '0';
$form['panes']['customer']['new_account']['#weight'] = '2';
$form['panes']['customer']['confirm_email'] = array(
'#title' => t('Verify E-mail address'),
'#type' => 'textfield',
'#size' => '32',
'#required' => true,
'#weight' => '1'
);
$form['#validate'][] = 'custom_code_validate_confirm_email';
}
}
function custom_code_validate_confirm_email(&$form, &$form_state){
if($form_state['values']['panes']['customer']['primary_email'] != $form_state['values']['panes']['customer']['confirm_email']) {
form_set_error('panes[customer][confirm_email', t('Email addresses must match.'));
}
}
/* end code for confirm_email */

With custom module, display result ,

function searchsong_block($op='list',$delta=0){
$block = array();
switch($op){
case "list":
$block[0][info] = t('Search song');
return $block;
case "view":
$block['subject']='THIS IS SONG SEARCH MODULE';
$block['content']=drupal_get_form('custom1_default_form');
return $block;
}
}
function custom1_default_form () {
$form = array();
$form['txt_name'] =
array('#type' => 'textfield',
'#title' => t('Please enter your name'),
'#default_value' => variable_get('webservice_user_url',''),
'#maxlength' => '40',
'#size' => '20',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Details'),
);
return $form;
}
function custom1_default_form_validate (&$form, &$form_state) {
if(($form_state['values']['txt_name']) == '') {
form_set_error('user_webservice', t('Enter a name'));
}
}
function custom1_default_form_submit ($form_id, $form_values) {
$GET_TXT_VAL = $_POST['txt_name'];
$result = db_query('SELECT title FROM {node} WHERE type = "%s" AND title LIKE "%%%s%%"', 'song', $GET_TXT_VAL);
$output='';
while ($row = db_fetch_object($result)) {
// drupal_set_message($row->title);----IF I ENABLE THIS LINE THEN MY SEARCH RESULT DISPLAYING IN THE GREEN BLOCK, YES I KNOW THIS FUNCTION SOMETHING LIKE ECHO JUST FOR TESTING PURPOSE WE SHOULD USE
$output .=$row->title;
}
$block['content'] = $output;
}
How to print my output ,
Above module does not display anything , even error also,
i thing i should use theme('itemlist') somthing , but i am not sure how to use this , and where i should use this ,
So what i want is , i want to display my search result , in the content region ,
Please find my question picture view below..
Validate and submit are not for outputting data.
You should show your results in custom1_default_form:
Add in submit $_SESSION['search_text'] or use multistep "storage" (learn drupal form api for this).
But let's look how to work via sessions:
Add in custom1_default_form:
// Here is your form code, so form will appear on the top
// ...
if (isset($_SESSION['search_text'])) {
//add here your code from submit that output searching result
$form['result'] = array(
'#type' => 'item',
'#value' => $output,
);
unset($_SESSION['search_text']); // don't forget clear session
}
Your submit function should be like this:
function custom1_default_form_submit($form, &$form_state) {
$_SESSION['search_text'] = $form_state['values']['txt_name'];
// this will store text field into session, then reload page,
// so you drupal_get_form will see entered values.
}
This is all.
Tips:
if(($form_state['values']['txt_name']) == '') {
form_set_error('user_webservice', t('Enter a name'));
}
This code is not needed, if you do:
$form['txt_name'] = array(
... // other properties
'#required' => true,
);
Read please about form api here: http://api.drupal.org
Other way to use block, just call function that output result and form via drupal_get_form, so:
...
$block['content']='custom1_default_result';
...
function custom1_default_result () {
$output .= drupal_get_form('custom1_default_form');
...
$output .= //search result if session filled/
}

Resources