Dropzonejs bypass useful core and contrib hooks

Created on 8 March 2024, 11 months ago
Updated 10 March 2024, 11 months ago

The original hook of the upload area of a media library is HOOK_form_media_library_add_form_upload_alter.

This hook is widely used on contrib and core areas to provide extra funcionality for the upload form, such as field_groups in order to support Media Library:

/**
 * Implements hook_form_media_library_add_form_upload_alter().
 */
function field_group_form_media_library_add_form_upload_alter(&$form, FormStateInterface $form_state) {

  // Attach the fieldgroups to the media entity form in Media Library widget.
  $storage = $form_state->getStorage();
  if (!empty($storage['media'])) {
    foreach ($storage['media'] as $delta => $media) {
      (...)
    }
  }

}

or the core theme Claro:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function claro_form_media_library_add_form_upload_alter(array &$form, FormStateInterface $form_state) {
  $form['#attributes']['class'][] = 'media-library-add-form--upload';
  if (isset($form['container']['upload'])) {
    ( ... )
  }
}

.

All of this functionality, which affect the whole form (and not only the upload file field), disappears when using dropzonejs as the hook changes from HOOK_form_media_library_add_form_upload_alter to HOOK_form_media_library_add_form_dropzonejs_alter.

The only workaround is to manually forward the new hook to the old one:

/**
 * Implements hook_form_FORM_ID_alter().
 *
 * The dropzonejs module modifies the form hook alter of the media library upload
 * area, preventing modules such as field_group from performing its operations.
 * We need to manually add the hooks to preserve the field group functionality.
 */
function uw_media_form_media_library_add_form_dropzonejs_alter(&$form, FormStateInterface $form_state) {
  field_group_form_media_library_add_form_upload_alter($form, $form_state);
}

I think this module should take care of ensuring that the additions made to the original file upload form on media library are not lost when using dropzonejs.

Feature request
Status

Active

Version

2.0

Component

Code

Created by

🇪🇸Spain idiaz.roncero Madrid

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @idiaz.roncero
  • 🇸🇮Slovenia primsi

    That is an implementation of hook_form_BASE_FORM_ID_alter for media library \Drupal\media_library\Form\AddFormBase::getBaseFormId. When you choose the Dropzonejs widget you basically choose a different form, which has its own unique form id.

    Or am I missing somthing?

    If the solution you found works for you, than I think that's the best you can do.

Production build 0.71.5 2024