Fix RichEditor type error when saving form without editing the field #18718
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes the
TypeError: Argument #2 ($rawState) must be of type ?array, string givenerror that occurs when saving a form that contains a RichEditor field in HTML mode (not using->json()) without editing that field.Related issues:
Problem
When a RichEditor stores content as HTML (the default mode), the
beforeStateDehydratedcallback registered insetUp()expects?array $rawState. However, when the form is saved without editing the RichEditor field:callAfterStateUpdated()is NOT called (since the field wasn't updated)callBeforeStateDehydrated()IS called (as part of the form save process)getRawState()returns the HTML string from the databaseThe existing fix in PR #17762 added state normalization to
callAfterStateUpdated(), which only runs when the RichEditor field itself is edited. This doesn't cover the case where other fields are edited but the RichEditor is left untouched.Solution
Add the same state normalization logic to
callBeforeStateDehydrated(), which runs during every form save regardless of which fields were edited. This ensures the raw state is converted to the expected array format before thebeforeStateDehydratedcallback is invoked.Test Case
->json()mode)TypeErroris thrown