2

using Drupal Commerce checkout form there is a default 'address' field (it's dynamic and includes several fields in one that are not easy to separate - such as country, name, address 1 and 2, state, etc.). I turned off the labels for the address field because they are ugly and I'd like to put the labels INSIDE each tag, as you would normally do with html like so:

HTML 5:

<input type="text" name="user" placeholder="Username">

Where the 'placeholder' property shows what should be entered into the field within the tag, thus removing the need to have a separate label at all.

Anyone know how to do this (include a placeholder property if you will) with hook_form_alter?

2

You can specify placeholder attribute in '#attributes' array like this:

function MODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'form_id') {
    $form['item']['#attributes']['placeholder'] = t('Placeholder text');
  }
}
  • Interesting. Seems reasonable but didn't work for me. I know I'm accessing the correct fields because I'm using the same field names as when I organized the fields by their '#weight' in my hook form alter module. For example, I have $form['customer_profile_billing']['field_title']['#attributes']['placeholder'] = t('title'); – Sage Jan 12 '16 at 1:39
  • 1
    Hmm, that's wired becouse It work's for me. Please, check this code - codepad.org/iAV0gkvG This is alter for node add form. And also try to check if field of array is valid name. Maybe use dsm() function from devel module to print $form, and check if everything is ok in this array. If it's maybe you have some other module which alter forms? Or some JS? – Marcin Jan 13 '16 at 12:46
  • $form['item']['#attributes']['placeholder'] = t('Placeholder text'); will work only for default fields. For custom fields, you need to specify like $form['item'][LANGUAGE_NONE][0]['#attributes']['placeholder'] = t('Placeholder text'); – Joel James Nov 23 '16 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.