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)

CommentFileSizeAuthor
#11 2898335-10.2.x-11.patch794 bytesludo.r
#10 2898335-10.patch794 bytesludo.r

Issue fork drupal-2898335

Command icon 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

MitraX created an issue. See original summary.

jakubmroz’s picture

Has this been resolved already in Drupal 8.5 or still pending?

Version: 8.3.5 » 8.3.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.3.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ludo.r’s picture

Title: Uploading file using managed_file field added to module-defined menu link's edit form causes ajax and database connection serialization errors » Using AJAX in menu link's edit form causes database connection serialization errors
Status: Active » Needs review
StatusFileSize
new794 bytes

This patch should fix the issue

ludo.r’s picture

StatusFileSize
new794 bytes

And here's a patch that applies for Drupal core 10.2.x

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests, +Needs issue summary update

Thanks 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.

jatingupta40 made their first commit to this issue’s fork.

jatingupta40’s picture

Opened MR

tcrawford’s picture

I 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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.