Problem/Motivation
Saving the Audit Trail settings form after the effective-delays AJAX preview has fired throws a fatal in validateForm():
Error: Typed property Drupal\audit_trail\Form\AuditTrailSettingsForm::$fileSystem must not be accessed before initialization in ...validateForm() (line 561).
A plain open-and-save works; it only fails once the form has been through an AJAX rebuild, so it looks intermittent.
Steps to reproduce
- Open the Audit Trail settings form.
- Change a cron-archive delay field (this fires the effective-delays #ajax preview, so the form is serialized into the form cache).
- Submit the form. validateForm() fatals as above.
Root cause
The form has #ajax, so Drupal serializes the form object into the form cache. FormBase uses DependencySerializationTrait, whose __wakeup() re-injects service properties by assigning them. The injected file_system property is declared private readonly. The wakeup assignment needs the property both writable and reachable: readonly forbids writes outside the constructor, and a private service property is not reliably re-injected (it serializes under a mangled class-scoped name). Either way the property is left uninitialized and validateForm() trips "accessed before initialization".
Proposed resolution
Declare the injected service protected and non-readonly so DependencySerializationTrait can re-inject it on wakeup:
protected FileSystemInterface $fileSystem,
More broadly, injected service properties on any serializable class that uses DependencySerializationTrait (forms with #ajax or multistep, controllers, plugins) should be protected and not readonly.
Issue fork audit_trail-3593033
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 #3
mably commentedComment #5
mably commented