Double output within Drupal function - drupal

I'm going crazy
function module_form_alter(&$form, $form_state, $form_id) {
// Nothing here
$var = 'bla-bla';
print_r($var);
// Nothing here
}
I see on the screen bla-blabla-bla
WHY?

You probably have two forms on the page. Try printing (better yet, install devel and use dpm) $form_id instead of $var and see which forms are involved.

hook_form_alter works on every form. You probably have the search form in this page, so it prints the text twice (one for every form).
In order to add changes to only one form, use the $form_id argument like this:
function module_form_alter(&$form, $form_state, $form_id) {
if($form_id == 'YOURFORMID') {
$var = 'bla-bla';
print_r($var);
}
}
change YOURFORMID to your form_id.
You can find the form_id by looking on the HTML of the form output and search the value of the input that his name is 'form_id'.

Related

Is there a way to override user_admin_account?

I need to override the user_admin_account function (which list users in backoffice).
Is there a way to do that without modify the core? I don't see any hook to do that.
That function is a form so you can implement hook_form_alter() in a custom module (or even a theme as it's an alter hook).
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_admin_account') {
$form['something']['#default_value'] = 'something else';
}
}

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');
}

How can I get the title of a form element in Drupal?

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']));
}
}
?>

drupal form alter

I'm developing a module which alter display of add/edit node forms. I'm a beginner in module development.
I have written following code, it is not working properly. Please tell me what's wrong with this?
function hook_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_form') {
drupal_set_message(t('some message.'));
}
}
This is for drupal 6.
In addition, the node add/edit forms have content-type specific IDs. so story nodes would be:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'story_node_form') {
drupal_set_message(t('Editing a story node!'));
}
}
If you're looking to catch every node edit form, regardless of type, try this:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if (isset($form['#node']) && $form_id == $form['#node']->type .'_node_form') {
drupal_set_message(t('Editing a node!'));
}
}
you should not call your function "hook_form_alter" but rather "yourmodule_form_alter". yourmodule should be the name of your module file, so if your module is called "hello" the function name should be "hello_form_alter".
You need to understand the hook system that Drupal uses if you want to do module development. When a hook is implemented somewhere in Drupal, it's called hook_something. The way this works is that every time a module wants to implement the hook, you need to replace the 'hook' with the 'modelname'. Drupal will know it's an instance of that hook and call it as such. Drupal also uses the same system in the theming layer, with a twist. As it will look at the different theming functions and prioritize them based on a ranking system, that enabled themes to override the way modules output data.
function hook_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'node_form') {
drupal_set_message(t('some message.'));
}
}
First you must "hook" keyword to your module keyword.. lets suppose your module name is "contact_us" then
function contact_us_form_alter(&$form, $form_state, $form_id) {
Now this function has three variable
$form
$form_id
$form_state
Most important variable during form alter is $form_id which basically note down which form is load in a page.
function contact_us_form_alter(&$form, $form_state, $form_id) {
print_r($form_id);exit; // Used to find the form id
}
after you find the form_id
function contact_us_form_alter(&$form, $form_state, $form_id) {
if($form_id=='contact_us_form')
// Do your stuff
}
Note:check you have properly write custom module and enable it.. also clear cache

Resources