Drupal hook alter with hook validate - drupal

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).

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 the submit function of the user_register form in 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.

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

Form submit handlers with additional arguments

For some requirement I need to pass additional information to form submit handler. In form api, while defining custom submit handler as
$additional_args = array();
$form['#submit'][] = 'my_submit_handler'
I expect to submit handler as
function my_submit_handler($form, &$form_state, $additional_args){
The submit handler is called by the drupal fapi, so you can't do something like that. Instead what you can do, is to add what you need, either to the $form, or to the $form_state. The usual approaches is to:
Added a field to the form, type value to store the value. Don't do this if you have the value in the form definition.
$form['store'] = array(
'#type' => 'value',
'#value' => $value
);
This will be available in $form_state['values']['store'].
Add the value to $form_state['storage'], done if you variables in your validation handle you want to transfer to your submit handler:
// Validation.
$form_state['storage']['value'] = $value;
...
// Submit
$value = $form_state['storage']['value'];
// Need to unset stored values when not used anymore.
unset($form_state['storage']['value']);
Drupal 7: Custom arguments are automatically propagated troug $form_state['build_info']['args']
This is said in http://api.drupal.org/api/drupal/includes!form.inc/function/drupal_get_form/7
Ex:
hook_form($form, &$form_state, $myAdditionnalArg) {...}
Then in
hook_form_submit($form, &$form_state) {
...
//$form_state['build_info']['args'] is an array containing at index 0 the value of argument $myAdditionnalArg
...
As reported in $form['#submit'] and $form['#validate'] and $form['#process'] no longer support custom parameters, the suggested way to pass parameters to a submission handler set as in the shown code is to use code similar to the following:
$form['#first_paramater'] = $value;
$form['#submit'][] = 'my_submit_handler';
The handler would retrieve the value as $form['#first_paramater'].
To notice that, instead of #first_paramater, the code can use a different string, but it must start with #.
Normally it's not necessary to set a submission handler like the code does, but there are some cases where it is necessary, like to alter a form created by another module, or to set a different submission handler for each of the submission buttons present in a form.
drupal_retrieve_form() saves the parameters passed to the form build handler in $form['#parameters'] which contains:
$form_id
$form_state
parameters passed to the form builder

Resources