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
First parameter in your form
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
Thank you for reminding. I
Thank you for reminding. I just test hook_form_FORM_ID_alter. It is working.
It's nice
Trying the same, but no luck
Hi,
I am trying to add the placeholder-attribute on the input field for my exposed filter.
I tried the following code block:
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!
<?php$form['title'][
Hashtag
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.
++Andy
Developing Drupal websites for Livelink New Media since 2008
You're right, I've updated my
You're right, I've updated my code accordingly. Thanks.
change your function and use
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.
It is my mistake. I got the
It is my mistake. I got the result
You forgot something...
Don't try to put a :
return $form;
at the end. ;)
how to reorder the field weight
Now the order is:
If I want to add a new field between "Your email address" (mail) and "Subject" (subject). like this
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?
In this case, or if your form
In this case, or if your form fields increase, you can easily set this code in your form_alter:
With this code you are controling weight order with $order array!
form alter
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
}
Note that dsm() will only
Note that
dsm()
will only work if you have the devel module enabled. If you do not have this module enabled, you can usedebug()
.form alter
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'),
);
}
}