You are here

function hooks_example_form_alter in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 hooks_example/hooks_example.module \hooks_example_form_alter()

Implements hook_form_alter().

Related topics

File

modules/hooks_example/hooks_example.module, line 193
Examples demonstrating how to implement and invoke hooks.

Code

function hooks_example_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  // This is an example of what is known as an alter hook. The $form parameter
  // in this case represents an already complete Form API array and our hook
  // implementation is being given the opportunity to make changes to the
  // existing data structure before it's used. Inovking and alter hooks is a
  // common pattern anytime lists or complex data structures are assembled.
  // hook_form_alter(), which allows you to manipulate any form, is one of the
  // most commonly implemented hooks.
  //
  // @see hook_form_alter()
  // @see hook_form_FORM_ID_alter()
  //
  // If this is the user login form, change the description text of the username
  // field.
  if ($form_id === 'user_login_form') {
    $form['name']['#description'] = t('This text has been altered by hooks_example_form_alter().');
  }
}