Problem/Motivation
When uploading a new file using the Media Library module, the "Add File" action fails to upload the file and silently throws a console error. This error occurs every time with every media type for our site.
Steps to reproduce:
- Choose a node that contains a media field for editing using the "Add media" button.
- In the resulting "Add or select media" lightbox, click the "Choose File" button in the "Add File" section.
- Choose a file of the appropriate media type.
- At this point, a progress spinner is shown then disappears. Nothing further occurs and the file is not added to the library.
- Check the console log to see the Ajax error:
An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: /media-library?media_library_opener_id=field%3Afield_documents_media_&media_library_allowed_types%5Bbasic_image%5D=basic_image&media_library_allowed_types%5Bdocuments%5D=documents&media_library_allowed_types%5Bicon%5D=icon&media_library_allowed_types%5Blocation_photos%5D=location_photos&media_library_allowed_types%5Bremote_video%5D=remote_video&media_library_selected_type=basic_image&media_library_remaining=1&hash=wFpjTCU6C4We9HKZWN4uK4B2cxaXeEhPCddqHR-vBKQ&media_library_content=1&_wrapper_format=drupal_ajax&ajax_form=1 StatusText: parsererror ResponseText: Notice: Undefined index: #parents in /app/web/core/modules/media_library/src/Form/AddFormBase.php on line 250 [...]
OS: nginx/1.14.2
Database: 5.5.5-10.1.38-MariaDB
PHP 7.2.15
Drupal 8.7.0
Proposed resolution
Adding a null check to AddFormBase.php:buildEntityFormElement():250 allowed me to successfully upload files (patch file to follow in comments).
- $parents = $form['#parents'];
+ if (array_key_exists("#parents", $form)) {
+ $parents = $form['#parents'];
+ }
+ else {
+ $parents = [];
+ }