Problem/Motivation
The idea behind the \Drupal\taxonomy_max_depth\Form\FormAltererBase::prepareFormCallback() method is really elegant and clever but the thing is that it creates a Closure which cause fatal errors when Drupal FormBuilder tries to push the form into cache as it tries to serialize it.
Steps to reproduce
- Install Drupal
- Enable taxonomy max depth and set in on a vocabulary
- Add a Media field with Media Library on the same vocabulary
- Try to create a term and to add it a media
Expected result: the Media Library opens and you can select one
Current result: the Media Library does not open and you can see a JS error in the console: Exception: Serialization "Closure" is not allowed in serialize()
Note: you can also try with a Paragraph field and try to add a paragraph item on your term.
Proposed resolution
Change the method to return a common callable and serializable array.
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | taxonomy_max_depth_serialization_error-3109174-2.patch | 2.38 KB | duaelfr |
Comments
Comment #2
duaelfrI'm not sure you'll be happy with this patch but it fixes the issue.
Adding the DependencySerializationTrait was necessary to prevent another serialization issue (Database service cannot be serialized and if a deep dependency of EntityTypeManager).
Comment #3
tresti88I came across this error after I added, to a vocabulary, an entity reference field that had unlimited cardinality. The Maximum ancestor depth was set to 2. The error appeared whenever i tried to add another entity reference item on the page.
Adding this patch fixed the issue for me.
Comment #5
dmitriy.trt commentedThank you for a patch! Indeed, this workaround I used before was not reliable, but
DependencySerializationTraitseems to be a proper way to handle such cases.Committed a bit changed version of the patch. Leaving this open mostly for myself, as it would be nice to write a functional test for this specific case before releasing a stable version. But patches with tests are welcome, as always!
Comment #9
dmitriy.trt commentedAdded JS tests to make sure the issue won't re-appear.