I am trying to create a module where the users creates his account and on submit, i get his information and insert them in a second database too.
I mean that he will exist in both databases and in Drupals user table and in user table of the other database.
How can i get his information and insert them to a custom database?
I am totally new to Drupal development.
Thank you in advance for any help or advice.
You will need to implement hook_form_alter() and use the following code:
function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "user_register_form")
{
$form['#submit'][] = "your_custom_submit_callback";
}
}
Then create the custom submit callback to manipulate the submitted values the way you like:
function your_custom_submit_callback($form, &$form_state)
{
// your code goes here...
}
Hope this works... Muhammad.
Related
I am trying to update form fields based on an uploaded file. I'm using hook_form_alter() to perform these updates, and I have found that I can change the default_value of a field when the webpage is first loaded. However, the same thing does not work when hook_form_alter() is called as a result of a file being uploaded. For instance,
function mymodule_form_alter(&$form, &$form_state, $form_id){
// I can change the default value here
$form['field_myfield']['und'][0]['value']['#default_value'] = "Some Value";
// Check to see if the upload button has been pressed
if (array_key_exists('clicked_button', $form_state))){
$trigger = $form_state['clicked_button']['#value'];
if($trigger == 'Upload'){
// Try to change the default value here. The value is changed in $form,
// but the field is not updated on the website.
$form['field_myfield']['und'][0]['value']['#default_value'] = "New Value";
}
}
}
Do I need to perform some sort of refresh? I briefly looked at AHAH, is this something I need to use? I am new to Drupal and I apologize in advance if there is something fundamentally wrong with my terminology and/or approach.
Any help is greatly appreciated.
Drupal 7 comes with the built-in user registration form (user/register). I use this form for new users to register. Which is quite obvious. Now the problem is, and I find it hard to believe that it isn't there, I need some validating.
When a new user completes the form and hits submit, the account is being created. Good.
But: when a user completes the form and hits submit, but the email address or username is already in use, the pages just reloads and the user is not being created, which is good, but theres no warning on what he has to change in the form what so ever.
I find it strange that this is not standard.
Could anyone provide me some help? I really have no clue...
Use can achieve that with many approaches.
Here's an example using hook_form_alter()
function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "user_register_form" || $form_id == "user_profile_form")
{
$form['#validate'][] = '_your_custom_validation_callback';
}
}
function _your_custom_validation_callback(&$form_state)
{
// use your validation code...
}
Hope this works... Muhammad.
I would like to make a form I have in my website self referencing. Or if that's not an option, how would I go for, for example, showing the results of a search I make in my site?
I have a site in which you search for places and it returns a list of places for your preferences. At the moment my script creates a new node every time a user searches but this isn't convenient anymore. How do I change it so that the page content is changed and I see the results instead of the search form?
Thanks,
You should redirect your form to a page passing a query string with the string of what the user searched and then use $_GET['search_param'] in your search/restuls page to handle what will be displayed to the user.
function yourform_form($form_state) {
$form = array();
//$form['your_search_field']
$form['#submit'][] = 'yourform_form_submit';
return $form;
}
function yourform_form_submit(&$form, $form_state) {
$query = 'search_param='. $form_state['values']['your_search_field'];
drupal_goto('search/results', query);
}
If you're using Drupal 7 your submit function should look like:
function yourform_form_submit(&$form, $form_state) {
$options['query']['search_param'] = $form_state['values']['your_search_field'];
drupal_goto('search/results', $options);
}
After you submit you should be redirected to http://yoursite.com/search/results?search_param=my_search_value
Note that this technique is used by popular search engines:
https://www.google.com/search?q=my_search_value
Your form should include $form['#action'] to lead you to specific page after submit:
function example_form($form_state) {
$form = array();
//your form code
//...
$form['#action'] = url('search/results');
return $form;
}
On submitting form (example_form_submit) you should take all your values and save them to cookies using user_cookie_save function and on your page you can use this cookies.
You also could serialize your values to deal only with one cookie if you want, and then unserialize them on your page. You can delete cookie using user_cookie_delete function.
You should define path search/results where you could take those cookies data and manipulate with it.
I am trying to setup drupal 7 so that was when users signs-in for the first time they are presented with a terms of use (I have tried the terms of use module but it doesn't suit my needs as it only seems to work when the user creates the account and in our instance all accounts are created by the admin), and as is standard the user has to agree to these terms inorder to use there account.
For the second part of this once the user logs and agrees with the terms they have to be presented with the edit profile form to make necessarily updates.
Does anyone know of anyway to set this up (preferably without alot of development as this project is short on time)??? Any help is greatly appreciated.
You know that user has not logged in yet if their "access" and "login" values are 0. So it should be enough to check them and if they are 0, redirect user to your T&Cs. In theory at least.
The easiest solution (that comes to my mind right now anyway) would be something like this:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_login') {
$form['#submit'][] = 'MYMODULE_user_login_submit';
}
}
function MYMODULE_user_login_submit($form, &$form_state) {
$user = user_load($form_state['uid']);
if ($user->access == 0) {
$form_state['redirect'] = 'your-terms-and-conditions-form-url';
}
}
hook into "user_login" form and add own submit function
in own submit function check both mentioned values, and if they equal 0, redirect user to your T&Cs
The thing is that our submit gets called after main user_login_submit(), where (well, not exactly there) "login" value already gets updated - therefore we can check "access" field.
The rest sounds easy enough - page with T&Cs and agree form, redirecting to user profile edit afterwards, easy-peasy...
I created one module in drupal name as newmodule.I use form alter to alter user registration form for adding one field location.When i submit the form, how i can get the value of the new field which i created .
To elaborate on the previous answers, somewhere in your hook_form_alter function, you want to tell the form to run your own #submit handler, e.g.:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
$form['new_field'] = array( ... ); // You already did this, right?
$form['#submit'][] = 'mymodule_submit_handler'; // Add this
}
Note: you should be appending on #submit here, not replacing it. Then in your submit handler, you can get the value easily, e.g.:
function mymodule_submit_handler($form, &$form_state) {
$value = $form_state['values']['new_field'];
}
The form value is stored in
$form_state['values']['field_name']
By default. If you set #tree to TRUE this behavior will change, and the values will be in a nested array instead of a flat one.
Two type of functions will be called, where you have access to the $form_state variable.
Validation functions is used validate the form data, to check if the user inputted data is acceptable, like a valid email address etc. To add a validation function add this in your form alter implementation:
$form['#validate'][] = 'name_of_your_validate_handler';
Submit functions is used to act upon a form with valid data. Typically you insert data into the database, set redirects and such here, to add a submit function add this in your form alter implementation:
$form['#submit'][] = 'name_of_your_submit_handler';
Both validation and submit functions take the same args:
function validate_or_submit_func(&$form, &$form_state) {
// $form is the form array created by drupal_get_form
// $form_state contains valuable info like, the submitted values and other options.
}
New module also needs a _submit hook invoked so you can dump out the $form and $form_state values so you can see them.