How to alter a form element's properties before rendering? Here's a simple task.

For accessibility reasons, from a custom module I need to perform alterations on textfield based form elements (such as email, textfield, etc ...) on custom forms displayed to front users.

These forms extends the Drupal\Core\Form\FormBase class.

The alterations I need to perform would be for exemple to automatically and programmatically copy the value of the field's placeholder text and put it in the field's title attribute (not the field's label, I'm talking about the HTML title attribute).

So the field's markup would look something like this :

&lt;input type="email" placeholder="Email" title="Email" /&gt;


I want to check all custom forms for the entire website.

I tried different methods :


hook_field_widget_form_alter : doesn't fire for fields on custom
forms because they are not widgets.
hook_preprocess_form_element : It's too late to perform these changes. The values just won't take effect on the field.
hook_form_alter : Well it's just too general, I find it a bit dodgy to parse the entire Render Array, check for fields, etc ... but may be it is the only way..


I looked into the Form API, Render API, Plugins API, I looked into services, nothing seems to offer any solution, unless of course I missing something, and I probably do.

Has anyone any idea on how to achieve this in the best Drupal 8 way?
In hook_template_preprocess_form_element the (input-)tags is already rendered.
You can use 

function hook/template_preprocess_input(&amp;$variables) {
  $variables['attributes']['title'] = 'my-title';
  $variables['attributes']['placeholder'] = 'my-placeholder';
}


to alter the input tag. use the same method for all other desired tags.
Here's a nice tutorial.


  You could make use of hook/template_preprocess to go a more generic
  way.