Add placeholder to form inputs when using Drupal’s Form API

Add placeholder to form inputs when using Drupal’s Form API

This is a quick one, thanks to Drupal’s Form API!
Use #attributes to set a placeholder or other attributes you need on an input.

function mymodule_test_form($form, &$form_state) {
  $form = array();
  $form['name'] = array(
    '#type' => 'text',
    '#title' => 'Your Name',
    '#attributes' => array(
      'placeholder' => 'First Name',
      'data-tooltip' => 'Enter your first name here',
    ),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

Comments are closed.