3

For the signup form I'm using the Mailchimp Signup module. The idea is to hide the label, which I did using the unset(); function, and put an a placeholder in the input field where people enter their emailaddress.

Here's the code:

function bw_custom_form_alter(&$form, &$form_state, $form_id){

  if ($form_id == 'mailchimp_signup_subscribe_block_mysite_group_form') {
    //dpm($form);
    unset( $form['mergevars']['EMAIL']['#title']);
    $form['mergevars']['EMAIL']['#placeholder'] = t('Your emailaddress');
    dpm($form['mergevars']['EMAIL']['#placeholder']);
    }
}

dpm($form['mergevars']['EMAIL']['#placeholder']); returns the string Your emailaddress nicely in the green drupal message frame. Yet, the actual placeholder isn't there in my input element. Looking at the attributes of the particular inputfield in question, the attribute isn't added either:

<input id="edit-mergevars-email" class="form-text required" type="text" maxlength="128" size="25" value="" name="mergevars[EMAIL]">

2 Answers 2

3

You have to use this:

$form['mergevars']['EMAIL']['#attributes'] = array(
 'placeholder'=> t('Your placeholder'),
);
0

If you want all field titles as placeholders:

$key_blacklist = array('#prefix','#suffix','#tree');

foreach($form['mergevars'] as $key => $var) {
  if ( ! in_array($key, $key_blacklist)) {
    $form['mergevars'][$key]['#attributes'] = array( 'placeholder'=> $form['mergevars'][$key]['#title']);
  }
}

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.