1

Been trying to add placeholder text for my link fields.

Using hook_form_alter to add the attributes. It works fine with text fields

$form['field_first_name']['und'][0]['value']['#attributes']['placeholder'] = t( 'Your First Name' );

But not able to get it work with link fields

$form ['field_twitter_link']['und'][0]['#attributes']['value']['placeholder'] = t( 'Your Twitter profile link' );

1 Answer 1

3

You could do something like this in

function mytheme_form_alter(&$form, &$form_state, $form_id){
  $form['#after_build'][] = 'mytheme_after_build'; 
}

function mytheme_after_build(&$form, &$form_state){
  $form['field_twitter_link']['und'][0]['url']['#attributes']['placeholder']=t('my new');
  return $form;
}

Taken from here https://drupal.org/node/1002560

4
  • 1
    Works great... feel bad i didn't check the issue queue properly. Thanks for your precious time.
    – jack
    Mar 31, 2014 at 19:34
  • I have a image field in the same form, when i upload an image it shows an error call to undefined function theme_after_build() and the upload fails.
    – jack
    Apr 4, 2014 at 5:24
  • @jack its working on my side. i am altering node form though.
    – Ali Nouman
    Apr 4, 2014 at 14:36
  • Hey human... I used it for user register form & was getting the alert. I changed #after_build to #pre_render, everything works good now.
    – jack
    Apr 5, 2014 at 10:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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