I am learning hook_form_alter(). I am trying to change "Contact" form. I want to change the title "Your name" to "Full name".

I create a module that is named "mymodule", only write the below code in "mymodule.module"

function mymodule_form_alter($form, &$form_state, $form_id) {
    $form['name']['#title'] = t('Full name');

When I access "/contact", it still shows "Your name". Then I installed Devel module and change below code

function mymodule_form_alter($form, &$form_state, $form_id) {
    $form['name']['#title'] = t('Full name');
    dsm($form);
}

I access "/contact" again. The below information is shown in message block

... (Array, 24 elements)
#attributes (Array, 1 element)
name (Array, 5 elements)
#type (String, 9 characters ) textfield
#title (String, 9 characters ) Full name <---------------
#maxlength (Integer) 255
#default_value (String, 5 characters ) admin
#required (Boolean) TRUE
......

It shows the title name is changed to "Full name", but the display is still "Your name".

Do I forget to set something or use wrong command?

Comments

drupso’s picture

First parameter in your form_alter declaration must be by reference. Put &$form instead $form.

Other thing, with this hook you are changing name title in all forms that this field exist. If you want change it only in contact form use a conditional in this hook, or use a hook_form_FORM_ID_alter method

sevenfish’s picture

Thank you for reminding. I just test hook_form_FORM_ID_alter. It is working.

function mymodule_form_contact_site_form_alter(&$form, &$form_state, $form_id) {
  	$form['name']['#title'] = t('Full name');
}

It's nice

ritagyam@dessci.com’s picture

Hi,
I am trying to add the placeholder-attribute on the input field for my exposed filter.
I tried the following code block:

function mymodule_form_alter(&$form, &$form_state, $form_id){
echo 'Debug';
  if($form_id == "views_exposed_form"){
    debug($form); // print $form array on the top of the page
    if (isset($form['title'])) {
            $form['title'][] = array('#placeholder' => t('placeholder value'));
    }
  }
}

I couldn't get it to work, so I tried using hook_form_FORM_ID_alter method, by modifying the above code. But still no luck. It is not even able to reach the function. Can anyone tell me what I might be doing wrong?
I am implementing these codes in template.php file and want to make sure if this is the right place to make changes?

Thanks!

Jaypan’s picture

$form['title']['attributes']['#placeholder'] = t('placeholder value');
kingandy’s picture

I am not 100% sure but shouldn't the hash be on the "attributes" property? Otherwise the Form API will assume "attributes" is a child object to be rendered, and won't have any idea what to do with the #placeholder property.

$form['title']['#attributes']['placeholder'] = t('placeholder value');

++Andy
Developing Drupal websites for Livelink New Media since 2008

Jaypan’s picture

You're right, I've updated my code accordingly. Thanks.

XandieL’s picture

change your function and use pass by reference

mymodule_form_alter(&$form, &$form_state, $form_id)

use the $form_id that was passed by reference to check if you are in the correct for you want to alter

inside the conditional statement

try to
dsm($form)

and check what you want to change.

I like the universe, but she messes with my words
I'm not talking planets or galaxies and the distance just makes it worse.
I know what you're thinking, this probably sounds rehearsed.

sevenfish’s picture

It is my mistake. I got the result

sebastiano0039’s picture

Don't try to put a :

return $form;

at the end. ;)

sevenfish’s picture

Now the order is:

Your name *
Your e-mail address *
Subject *
Message *

If I want to add a new field between "Your email address" (mail) and "Subject" (subject). like this

Your name *
Your e-mail address *
new field <---------------
Subject *
Message *

I know change weight of "name", "mail". If one form have many fields before the "new field", should I change every field weight before it?

drupso’s picture

In this case, or if your form fields increase, you can easily set this code in your form_alter:

$order = array('your_first_field_name','your_second_field_name', .... , 'your_last_field_name' );
//set a weight for each field 
foreach ($order as $key => $field) {
    $form[$field]['#weight'] = $key;

  }

With this code you are controling weight order with $order array!

surendra77c’s picture

Hi ,

First check what your contact form id is by dsm($form_id)

if ($form_id == 'search_block_form') {
// here your logic will come to alter the name or title of the form
}

Jaypan’s picture

Note that dsm() will only work if you have the devel module enabled. If you do not have this module enabled, you can use debug().

sahil432’s picture

set
package :Core
in info file

and then write in your module file
//first method
use Drupal\Core\Form\FormStateInterface;
function hook_form_FORMID_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form['phone_number']['#title'] = t('Start with + and your country code.');

}
//Second Method--------------------------------------------
function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id)
{
if($form_id == 'user_login_form')
{
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('name'),
);
}

if($form_id == 'second_form')
{
$form['phone_number'] = array(
'#type' => 'tel',
'#title' => t('name'),
);
}
}