After submit action in form_alter - drupal

i need to insert data in my tables after user creation. I think to use hook_form_alter() for $form_id == "user_register" but I don't know how to say "after you created user, do this."
How can I do it in hook_form_alter()?

You can add custom submit handler to forms like this.
function hook_form_user_register_alter(&$form, &$form_state) {
// ...
$form['#submit'][] = 'yourModule_user_register_submit';
}
function yourModule_user_register_submit($form, &$form_state) {
// do what you want to do after registration
}
I'd also recommend to use Drupal's Triggers & Actions to achieve this. AFAIK there was a bug with one of the triggers that fire after user registration. Don't know if that has been fixed.

Related

How to logout and display message after login under certain condition?

Given a Drupal 7 website I want to customize log in behavior: When a users logs in I want to check if they are in a blacklist. If they are in that list I want them to be automatically logged out and told about the reason they are being kicked out. So In one of the custom modules of this drupal I have add the following hook:
function mymodule_user_login(&$edit, $account) {
if(blacklist(&$edit, $account)) {
drupal_goto("/user/logout/");
drupal_set_message('Acces denied','error');
}
}
However, my code doesn't work. What I'm doing wrong? I'm sure the hook is executed because I checked this using a watchdog. However, I also discovered the hook only gets executed if admin is the user who is logging in.
Are there any alternatives (maybe using Context module)? Any suggestions would be aprreciated!
Thanks!
Your code does not work because drupal_goto() calls drupal_exit() so the rest of your script does not get executed.
What you are trying to do will not work because user_logout() itself calls drupal_goto() to the front page.
You can however add an extra validation callback in the user login form so you can prevent users from logging in altogether.
<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login':
case 'user_login_block':
$form['#validate'][] = 'mymodule_custom_user_validation',
break;
}
}
function mymodule_custom_user_valudation($form, &$form_state) {
if (// Add your blacklist conditons here. ) {
form_set_error('', t('Your account is blacklisted, therefore you cannot login to this account'))
}
}
?>
If your interested in using the Login Destination Module, then you could create some type of rule to hook into your blacklist check and then redirect that user to the "user/logout" path. Let me know if you have any questions.
What exactly is your "blacklist()" function doing?

Change form data after submit in drupal

in some content type form I added a checkbox. When this checkbox is checked I want to remove some of the submitted data.
To do so I created a custom module (my_module.module):
function my_module_form_alter(&$form, &$form_state) {
// ...
$form['#submit'][] = 'my_module_form_alter_submit';
}
function my_module_form_alter_submit($form_id, $form_values) {
drupal_set_message(t('Submit Function Executed!'));
}
How can I tell this module to refer only to the form of a certain containt type? And how can I remove data when it is submitted?
Assuming you are altering a node edit form, you can either conditionally add the submit callback such as (in your hook_form_alter):
if(isset($form['#node']) && $form['type']['#value'] == 'page') {
$form['#submit'][] = 'my_module_form_alter_submit';
}
or, you could check the $form argument in the submit callback in a similar fashion.
You are missing the third argument to the hook_form_alter which should be $form_id, and your submit callback should take the arguments such as:
function my_module_form_alter_submit($form, &$form_state) { ... }
See also:
http://api.drupal.org/api/drupal/developer%21hooks%21core.php/function/hook_form_alter/6
http://api.drupal.org/api/drupal/developer%21topics%21forms_api.html/6
To remove data after the submit, in your form_alter function, just use unset() on the form field. unset($form['my-field']);

Close Overlay and Redirect to Custom URL on Save-Node in Drupal 7?

When my node form saves, I want to close the admin overlay and redirect to a custom URL that is stored with the node. hook_form_alter() is setting $form['#redirect'] but I think that would only work with no admin overlay.
I have never used it before myself, but i think you can call the overlay_close_dialog(...) function from your hook_submit
See http://api.drupal.org/api/drupal/modules--overlay--overlay.module/function/overlay_close_dialog/7 for more info
Kept googling my way here as well.. this is the working solution:
Inside your form:
$form['somebutton']['#submit'] = array('your_custom_callback');
Add a custom callback
function your_custom_callback($form, &$form_state) {
//redirect users to Drupal.org
$url = "http://drupal.org";
if (module_exists('overlay') && overlay_get_mode() == 'child') {
unset($_GET['destination']);
overlay_close_dialog($url, array('external' => TRUE));
$form_state['redirect'] = FALSE;
} else {
$form_state['redirect'] = $url;
}
}
Kept googling my way here and wanted to post a final solution that worked for me on Drupal 7 (got here thanks to jakraska's suggestion). First use hook_form_FORM_ID_alter() to hook into the specific form you want to alter
/**
* Implementation of hook_form_FORM_ID_alter()
*
**/
function mymodule_form_FORM_ID_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_callback';
}
Then write your callback that will render the page. In my case I simply wanted to close the overlay, so that's why I used the $form_state['redirect'] = FALSE;
function mymodule_callback(&$form, &$form_state) {
// Form API will re-render the current page and pass the redirect information to the overlay JavaScript
overlay_close_dialog();
// stay on the same page after all submit callbacks have been processed.
$form_state['redirect'] = FALSE;
}
If you want to redirect to another path,
See https://api.acquia.com/api/drupal/modules!overlay!overlay.module/function/overlay_form_submit/7 - I believe that should get you there.
Make sure to use $form_state['rebuild'] = TRUE; inside your hook_submit function, otherwise it would not close overlay after form is submitted.

Drupal: run custom code when a form is submitted

how can I run specific code when a form is submitted in Drupal ?
I'm using hook_form_alter to edit the form, but I actually need to collect the data inserted by user and run code when the user click on "Save / Register" etc
thanks
You can add callbacks to the submit array. It goes something like this:
function myform_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'some_form') {
$form['#submit'][] = 'mycallback';
}
}
function mycallback(&$form, &$form_state) {
// do stuff
}
Try adding the following function:
function myform_form_submit($form_id, $form_values){
print_r($form_values);
// custom code
}
worked for me. Hope it helps :)

Drupal 6 editing the submit function on a content type

I have a content type and I wish to edit the submit function. I thought the way you would do this would be as follows:
function moduleName_contentType_node_form_submit($form, &$form_state){
drupal_set_message(t('Test'));
}
I cleared the cached but the message is not being displayed on the screen. Am I doing this correctly or do I need to use form_alter? If so how would I do that?
In this case too you can use form alter, and add
$form['#submit'][] = 'your_sumbmit_callback';
or if you want to completely change the submit and do your own thing:
$form['#submit'] = array('your_submit_callback');
And obviously the callback function needs to be defined;
function your_submit_callback( $form, &$form_state) {
drupal_set_message('hello');
}

Resources