Customizing existing forms in Drupal
The Form API does not just provide a way to create forms. There are ways to alter existing forms through hooks in a custom module. By using this technique, new elements can be added, default values can be changed, and elements can even be hidden from view to simplify the user experience.
The altering of a form does not happen in a custom class; this is a hook defined in the module file. In this recipe, we will use the hook_form_FORM_ID_alter() hook to add a telephone field to the site’s configuration form.
How to do it…
- Ensure that your module has a 
.modulefile to contain hooks, such asmymodule.module. - We will implement 
hook_form_FORM_ID_alterfor thesystem_site_information_settingsform:<?php
use Drupal\Core\Form\FormStateInterface;
function mymodule_form_system_site_information
  _settings_alter(array &$form, FormStateInterface
    $form_state) {  // Code to alter form...