Media Library provides a class, \Drupal\media_library\Form\AddFormBase, which allows modules to easily implement forms for adding media items directly to the media library.
Before #3087456: Move some representational classes in Media Library to Classy and others to Seven and/or Claro, as appropriate, AddFormBase implemented the getFormId() method that all forms must implement. This inadvertently made it more difficult to alter any forms derived from this class, because they would all share the same form ID (media_library_add_form). hook_form_alter implementations would need to detect the class being used to build the form in order to target a specific version of AddFormBase, like so:
function mymodule_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
if ($form_id === 'media_library_add_form') {
$form_object = $form_state->getFormObject();
if ($form_object instance \Drupal\media_library\Form\FileUploadForm) {
// This is the file upload form provided by core
}
elseif ($form_object instance \Drupal\media_library\Form\OEmbed) {
// This is the oEmbed add form provided by core
}
}
}
Now, AddFormBase is a base form and provides a base form ID (media_library_add_form), but not an individual form ID. Subclasses of AddFormBase must implement getFormId() or they will cause fatal PHP errors.
This makes it possible to target all variants of AddFormBase using hook_form_BASE_FORM_ID_alter():
function mymodule_form_media_library_add_form_alter(array &$form, FormStateInterface $form_state) {
// This will target any variant of AddFormBase
}
You can modify individual variants of AddFormBase in the normal way, using hook_form_alter() or hook_form_FORM_ID_alter(). The two AddFormBase subclasses that ship in core have new form IDs:
\Drupal\media_library\Form\FileUploadForm: media_library_add_form_upload
\Drupal\media_library\Form\OEmbedForm: media_library_add_form_oembed