override the submit function of the user_register form in Drupal - drupal

I am working on a module that connects Drupal to an external Web Service.
When a user registers, I want him to register in the web service and not in Drupal.
I want to override the submit function that is called when the user_register form is submitted.
I already use $form['#submit'], but its not working.
Any other solution? I am using drupal7.
I used like that
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#title'] = t('Full name'); // perform other changes here
$form['#submit'][] = 'mymodule_user_register_form_submit'
}

When you use
$form['#submit'][] = 'mymodule_user_register_form_submit'
you are not actually overriding the submit module. You are saying after executing the submit function created by all other modules(which were run before mine), also execute my function mymodule_user_register_form_submit
If you want to override the submit function use something like $form['#submit'] = 'mymodule_user_register_form_submit' which will make sure that your function is the only function that will get executed.

Related

WP Shortcodes not being added

I was creating a wordpress plugin where the user enters in some information, and is able to generate shortcodes. I was not quite sure where the shortcodes are supposed to go - my current setup is class-based, and I want to be able to create a shortcode when an AJAX request is being made, and is successful. The following two methods are in the same file in the class.
This method gets called via the admin-ajax.php file:
public static function processAjax()
{
global $wpdb;
$event_data = $_POST['obj'];
$event_title = $event_data[0];
$event_subdomain = $event_data[1];
$result_events = $wpdb->get_results("SELECT * FROM wp_shortcode_plugin WHERE subdomain = '{$event_subdomain}'", OBJECT);
if (sizeof($result_events)>0) {
echo "duplicate";
} else {
add_shortcode($event_subdomain, 'getEmbed');
$results = $wpdb->insert('wp_shortcode_plugin', array("event_name"=>$event_title, "subdomain"=>$event_subdomain));
echo json_encode($_POST['obj']);
}
die();
}
And here is my getEmbed() method that I would like to call.
public static function getEmbed()
{
return 'test';
}
It seems that the shortcodes are not being created, however. Am I missing something here? Also, is it possible to pass a value to the getEmbed function from the add_shortcode() method?
Instead of adding shortcode directly from AJAX, you should use update_option to store in the information for the shortcode to be loaded. If option doesn't exist, it will be created.
Than you will simple use wp_init hook to load up all of the shortcodes you need to load in the function.php file for the theme or plugin php file.
You should use get_option within the wp_init hook and check the values in there. You will need to have function(s) associated with the shortcodes, which can be autogenerated in php using create_function or you can route them through 1 function (defined in your php file) that will have the $atts and $content parameters defined and do whatever depending on the value of your get_option that you send to that function.
add_shortcode function should be defined within the wp_init hook, after checking the value of the get_option function. You will need to give your option a name and add to it via the ajax function. the option will most likely want to be an array, that wordpress will automatically serialize. Than you use that array returned from get_option to loop through the array of shortcodes, and call add_shortcode as many times as you need there. This requires setting up your option array so that it has a shortcode tag defined in each index of the array. I would, personally, make the shortcode tag the key of the array and all attributes of the shortcode, imo, would than be an array of that array.
Hope this helps you to get started on this.

Drupal 7 - Change field widget type based on form status (add/edit)

I have an entity reference field using Autocomplete widget type. I´d like to change the widget type to 'select list' for adding new node, and keep autocomplete when editing.
Have been two days I´m working on this. I didn´t found any solution.
One way I've done this is using hook_form_alter. Create a custom module (if you don't have one already (let's call it mymodule for now) and add the function:
function mymodule_form_alter(&$form, &$form_state, $form_id)
In there you can check the id to see which form is being processed, it should be something along the lines of mytype_node_form but you can also inspect it fairly easily by executing something like drupal_set_message(print_r($form_id, true)); in the function.
You can check to see if you're adding or updating by checking $form_state['node']->nid. After that you can modify the form by doing something like the following:
function mymodule_form_alter(&$form, &$form_state, $form_id)
{
// check to see if this is our form and it is a new node form (doesn't have an id yet)
if ($form_id == 'mytype_node_form' && !isset($form_state['node']->nid)) {
$form['field_coordinators']['und']['#type'] = 'select';
}
}
That's just a start mind you, you'll likely have to change or even remove other properties but you can look into these settings by setting the field to use a select list and inspecting the structure of $form, again.

Override user_login_submit in Drupal 6 to redirect an authenticated user

I would like to redirect a user that logged in over the user login block. What I have is a module that contains the following code:
Appendix, 3.9.2011, 15:30h: Changed code according to the advice of kiamlaluno.
Appendix, 3.9.2011, 17:08h: Small Fix: Changed node/admin to admin.
Appendix, 3.9.2011, 17:24h: removed []
-> code is working like this now, but do not forget to change the module priority in DB.
function _MYMODULE_user_login_submit($form, &$form_state) {
global $user;
if ($user->uid == 1) {
$form_state['redirect'] = 'admin';
} elseif ($user->uid) {
$form_state['redirect'] = 'node/add/image';
return;
}
}
/**
* Modifies the outfit and behaviour of the user login block.
*/
function MYMODULE_form_user_login_block_alter(&$form, $form_state) {
unset($form['#action']);
// removes the forgot password and register links
$form['links'] = array();
// Redirects the user to the image upload page after login
// This cannot be done by a rule, the rule based redirect is only
// working for the login page not the user login block.
$form['#submit'] = array('_MYMODULE_user_login_submit');
}
It doesn't redirect users; it seems like _MYMODULE_user_login_submit() is simply ignored.
What I know already:
I cannot use Rules/Triggers, because I do not login over the login page but the user login block
It is always said: "use logintoboggan" on posts, but there I only have redirection options for "on registration" and "on confirmation", but I need "on authentication" or "after login".
Anyway, I do not want to use more modules, I prefer a few lines of PHP.
Your code doesn't work because user_block_login() sets the "#action" property for the form; in that case, redirecting the form after submission doesn't work.
$form = array(
'#action' => url($_GET['q'], array('query' => drupal_get_destination())),
'#id' => 'user-login-form',
'#validate' => user_login_default_validators(),
'#submit' => array('user_login_submit'),
);
To make it work, you should first unset $form[#action], and then executing the code you already execute in your hook_form_alter() implementation.
As side notes, I will add:
If you want to be sure your code effectively redirect the user where you want, be sure your module is executed for last; if any other module that implements hook_form_alter() adds a form submission handler to redirect the user to a different page, and that module is executed after yours, then your module would not have any effect. To make sure your module is executed after the others, you should use code similar to the following during the installation of the module, or in an update hook. (Replace "MYMODULE" with the short name of the module.)
db_query("UPDATE {system} SET weight = 100 WHERE name = 'MYMODULE');
Instead of using MYMODULE_form_alter(), you can use `MYMODULE_form_user_login_block_alter(), which would not require to check the form ID.
You should append new form submission handlers, instead of replacing the existing ones. This means you should use $form['#submit'][] = 'user_login_submit_redirected';.
Functions implemented in a module should be prefixed with the short name of the module, which means "MYMODULE_" or "_MYMODULE_" (the latter is for private functions). Not using such prefix could create a compatibility issue with other module, such as the User module, as the function you are using has a name starting with "user_."
can u try this please
function user_login_submit_redirected($form, &$form_state) {
global $user;
if ($user->uid == 0) {
$form_state['redirect'] = 'node/admin';
drupal_goto('node/admin') ;
} elseif ($user->uid) {
$form_state['redirect'] = 'node/add/image';
drupal_goto('node/add/image') ;
return;
}
}

How to pass variable from form submit function to page callback function in Drupal 6

I have a Drupal form and a submit function that processes that form. I would like to pass data from the submit function to the page callback function that loads after the form was processed.
Well, 10 months later of Drupal development later, I believe the $_SESSION variable is the only way of doing this. Drupal doesn't have any special tools for this.
It's a bit "late" but I feel to propose a new solution as I just found this post. Submit and Callback functions share the same $form and $form_state variables. If you want to pass a variable from one to another you can do it this way:
function YOUFORM_submit($form, &$form_state) {
// Set the variable
$form_state['values']['NEW_VAR'] = NEW_VALUE;
}
function YOUFORM_callback($form, &$form_state) {
// Get the variable
$new_value = $form_state['values']['NEW_VAR'];
}
More informations here : $form_state keys

Drupal hook alter with hook validate

I have a CCK defined form called 'mytype_node_form'.
I create a module called form_overrides
I create a function called form_overrides_form_alter where I successfully check for $form_id = 'mytype_node_form'
Question:
What should be the name of my validation function (hook_validate) in module form_overrides that would allow me to add custom validation to form mytype_node_form?
I thought it should look something like this
function form_overrides_mytype_node_form_validate($form, &$form_state) or
function form_overrides_validate($form, &$form_state)
If you don't specify the validation function in $form['#validate'], then Drupal looks for a function named with the form ID plus "_validate". So in this case it would be looking for mytype_node_form_validate() (and similarly mytype_node_form_submit() for the submit function).

Resources