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']);
Related
I have a custom content type called events which has a few fields defined in it.
The field name is field_store_name. I can get all the options from these check boxes using this code:
$form['field_store']['und']['#options']
This is how I get the option(s) that are selected/checked. Is this the correct way of doing this?
$form_state['build_info']['args']['0']->field_store['und']
Thanks
When user submits form your custom submitter could be called.
To add custom form submitter to any form you should use:
/* Implements hook_form_alter(). */
function moduleName_form_alter($form, $form_state) {
// ...
$form['#submit'][] = 'moduleName_submitterName';
// ...
}
So in custom submitter you will have all submitted values under $form_state['values']:
function moduleName_submitterName($form, $form_state) {
dpm($form_state['values']);
}
This index will apper in $form_state array only when you submit form and will contain submitted values. $form array will still contain default values shown at form before you've changed them and submitted form.
Read more:
An example of form submitter: https://www.drupal.org/node/717740.
hook_form_alter(): https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_form_alter/7
Value you need should be $form_state['values']['field_store]['und'][0].
hey i got a contnet type that i made and i want to twweak it some , when i do form alter its all good but once i do submit its ignore my changes that i added a field for example, how can i do that changes and tell him to insert also that field? do i need to put all the fields again in hook_submit?
$function YOURMODULE_form_alter(&$form, &$form_state) {
//...
$form['#submit'][] = 'YOURMODULE_submitfunction';
//...
}
function YOURMODULE_submitfunction($form, &$form_state) {
// Save your own changes here to DB or something other
}
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.
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');
}
For example, in the registration form, there is "Username" and the text field for it which has the input type="text" name="name" ....
I need to know how can I get the title from the input field's name.
I'm expecting a function like:
$title = get_title_for_element('name');
Result:
assert($title == 'Username'); // is true
Is there something like this in Drupal?
Thanks.
You have the form and the form state variables available to your validation function. You should use form_set_error() to set the error.
There is no function that I am aware of which will map from the values array to the form array. But it is not dificult to work it out. Understanding the form data structure is one of the key skills you need when building drupal.
In this case the form in question is generated (in a roundabout way) by user_edit_form, you can see the data structure in there.
$form['account']['name'] is the username field. and the array key for the title is '#title' as it will be in most cases for form elements.
You can do it in two different ways as I see it. Let's create a module called mycustomvalidation.module (remember to create the mycustomvalidation.info file also).
Note: The code below has not been tested, so you might have to do some minor adjustments. This is Drupal 6.x code by the way.
1) Using hook_user()
What you need is a custom module containing your own implementation of hook_user() http://api.drupal.org/api/function/hook_user/6.
<?php
function mycustomvalidation_user($op, &$edit, &$account, $category = NULL) {
if ($op == 'validate') {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($edit['profile_fullname'] != '') {
form_set_error('profile_fullname', t("Field 'Fullname' must not be empty."));
}
}
}
?>
2) Using form_alter() and a custom validation function
Personally, I would go for this option because I find it cleaner and more "correct". We're adding a custom validation function to our profile field here.
<?php
function mycustomvalidation_form_alter(&$form, $form_state, $form_id) {
// Check if we are loading 'user_register' or 'user_edit' forms.
if ($form_id == 'user_register' || $form_id == 'user_edit') {
// Add a custom validation function to the element.
$form['User information']['profile_fullname']['#element_validate'] = array('mycustomvalidation_profile_fullname_validate');
}
}
function mycustomvalidation_profile_fullname_validate($field) {
// Checking for an empty 'profile_fullname' field here, but you should adjust it to your needs.
if ($field['#value'] != '') {
form_set_error('profile_fullname', t("Field %title must not be empty.", array('%title' => $field['#title']));
}
}
?>