I'm on Drupal 8. I have created a new media bundle (permissions_json) which can be created from /media/add/permissions_json, where administrator user can upload specifically formatted JSON files, and I can upload to it all fine and dandy.

I'd like to validate the file uploaded and rename it. The problem is that I can't find a way to hook into the form. Implementing hook_form_FORM_ID_alter() with gp_mcs_media_permissions_json_add_form_alter() or gp_mcs_form_media_permissions_json_add_form_alter() doesn't have any effect. Even the following implementation didn't have any effect.

function gp_mcs_form_alter(&$form, &$form_state, $form_id) {

If I put a simple exit() on the module's page stops the website, so the file is accessible. I just can't figure out how to add validation to this add media form.

How can I alter the form to add a new validation handler?


1 Answer 1


The form ID for an entity is a string that is usually returned from EntityForm::getFormId(), if the class used for the form doesn't override that method.

This means that, by default, the form ID follows the <entity>_<bundle>_<operation>_form schema, where the <operation> part is omitted, when operation is default.
Since the Media module doesn't have a class that overrides EntityForm::getFormId(), the correct ID for the form that creates a new Media entity whose bundle is permissions_json is media_permissions_json_add. This means that a module whose machine name is gp_mcs can alter that form by implementing hook_form_FORM_ID_alter() with a function named gp_mcs_form_media_permissions_json_add_alter. (Note that the form ID is between form and alter.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.