Put Cursor Automatically in the text input area

This should work on both Drupal 7 and drupal 8.  I have tested for drupal 8 and working fine.

$form['textfield'] = array(
  '#type' => 'textarea',
  '#attributes' => array(
  'id' => 'unique-textarea'
  'autofocus' => TRUE,

Jquery way to do it.


Remember to put this after complete page load.


Disable Form Submit after click to prevent multiple submissions.

For Drupal 7, Hide Submit Module can be used.

Features of ‘Hide Submit’:

  1. Hide or disable the submit button after it has been clicked
  2. Display a message and/or image while waiting

For Drupal 8, the above module is not present. This piece of code will work.

$form['submit_button'] = array(
  '#type' => 'submit',
  '#value' => 'Save',
  '#attributes' => array(
  'onclick' => 'javascript:var s=this;setTimeout(function()      {s.disabled=true;},1);',

How to make a form element required with states ?

So the problem is when you try to make a field required using ‘states’ , it is not getting validated because everything happens in the browser (client side), nothing get validated on server side. So if you try this code.

'required' => array(
':input[name="save_settings"]' => array('checked' => TRUE),

This will add the red asterisk to the form element but it will not get validated. So the solution is

You need to add custom validation in hook_form_validate function.

Reference : https://api.drupal.org/api/examples/form_example%21form_example_states.inc/function/form_example_states_form/7