ISSUE SUMMARY:
If you add custom managed field to the module-defined menu items via hook_BASE_FORM_id_alter, uploading a file is broken.
ENVIRONMENT:
OS/web server: Linux/Apache
Drupal version: 8.3.5
REPRODUCIBILITY: always
STEPS TO REPRODUCE:
1) Create a custom module (e.g. mymodule)
2) Implement hook_BASE_FORM_id_alter in the .module file in order to add new filed to the menu item edit form, with the following code:
/**
* Implements hook_BASE_FORM_id_alter().
*
* Alter menu link edit form for special menu item type (e.g. Home).
*/
function mymodule_form_menu_link_edit_alter(&$form, &$form_state, $form_id) {
$form['item_image'] = [
'#type' => 'managed_file',
'#title' => t('Item image'),
'#upload_location' => 'public://item_image',
'#default_value' => 0,
];
}
3) Navigate to the menu edit form, e.g. Home link in Main Navigation or Login/Logout link in User Menu. For example, Administration > Structure > Menus > Main navigation > Home edit. The menu item edit form will be displayed containing the item image field, you've created in the hook function.
4) Try to upload an image
EXPECTED BEHAVIOR:
An image file should be uploaded without any issue, just like in any other form.
ACTUAL BEHAVIOR:
Uploading image to the field is broken, causing the an Ajax and database serialization errors
Browser errors details:
POST http://localhost/d8/develop/admin/structure/menu/link/standard.front_pag... 500 (Internal Server Error) jquery.min.js?v=2.2.4:4
Uncaught
Drupal.AjaxError {message: "↵An AJAX HTTP error occurred.↵HTTP Result Code: 50…encySerializationTrait as a temporary solution."}", name: "AjaxError"}
message: "↵An AJAX HTTP error occurred.↵HTTP Result Code: 500↵Debugging information follows.↵Path: /d8/develop/admin/structure/menu/link/standard.front_page/edit?element_parents=item_image&ajax_form=1↵StatusText: Internal Server Error↵ResponseText: {"message":"A fatal error occurred: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution."}"
name
:
"AjaxError"
Recent log errors details:
Type: php
Date: Friday, July 28, 2017 - 16:52
User: admin
Location: http://localhost/d8/develop/admin/structure/menu/link/standard.front_pag...
Referrer: http://localhost/d8/develop/admin/structure/menu/link/standard.front_pag...
Message LogicException: The database connection is not serializable. This probably means you are serializing an object that has an indirect reference to the database connection. Adjust your code so that is not necessary. Alternatively, look at DependencySerializationTrait as a temporary solution. in Drupal\Core\Database\Connection->__sleep() (line 1471 of /opt/lampp/htdocs/d8/develop/core/lib/Drupal/Core/Database/Connection.php).
Severity: Error
Hostname: 127.0.0.1
NOTES:
- The issue not tested on Windows platform
- Upload destination has valid permissions, so uploading files in other forms works without problems
- Modyfing menu item content form in the same way works without any issue (e.g. mymodule_form_menu_link_content_form_alter)
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | 2898335-10.2.x-11.patch | 794 bytes | ludo.r |
| #10 | 2898335-10.patch | 794 bytes | ludo.r |
Issue fork drupal-2898335
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
jakubmroz commentedHas this been resolved already in Drupal 8.5 or still pending?
Comment #10
ludo.rThis patch should fix the issue
Comment #11
ludo.rAnd here's a patch that applies for Drupal core 10.2.x
Comment #12
smustgrave commentedThanks for working on this
Patches should be in MRs now for tests to run.
Issue summary appears incomplete, should be following the standard template
Will most likely need test coverage
Thanks.
Comment #15
jatingupta40 commentedOpened MR
Comment #16
tcrawford commentedI tried the above MR (#15), but still see that the connection is being serialized. Moreover, the issue only occurred while editing the view in German (the site's default language), but not English.
Comment #18
quietone commentedUpdating tags per Issue tags field and Issue tags -- special tags and for issue #3565085: Drupal core issue tag cleanup.
Comment #20
phthlaap commentedWrote and validated a new test for the fix.
Comment #21
smustgrave commentedLeft some small comments around the test.
Can the summary be cleaned up some as it difficult to read. Recommend switching to the standard template.
This is super close! Tagging for novice as the changes I requested as super small @pthlaap feel free to continue working on it.
Comment #23
brandonlira commentedHi @smustgrave,
I shortened the test docblock and removed the custom assertion message as requested. I also limited the allowed classes for unserialize() so the test follows Drupal coding standards.
Please let me know if anything else needs adjusting.
Thank you!