Don't update $this->entity in EntityForm::afterBuild()

Created on 6 October 2016, almost 9 years ago
Updated 9 September 2025, 8 days ago

Problem/Motivation

Back in #2448357: Config entity forms need to have access to an updated entity object at all times β†’ (committed in March 2015 as 1c72fda) an #after_build callback was introduced entity forms to update $this->entity with the input values when the form is being rebuilt and input is being processed.

The callback is being explicitly unset in ContentEntityForm. The reasoning is given in the code

+    // Content entity forms do not use the parent's #after_build callback
+    // because they only need to rebuild the entity in the validation and the
+    // submit handler because Field API uses its own #after_build callback for
+    // its widgets.

However, WidgetBase::afterBuild() does not update $this->entity. This leads to inconsistent behavior between config and content entities, in particular when dealing with Ajax.

Proposed resolution

Try removing the unsetting of the #after_build and see what breaks.

Following things have been identified:

  1. OptionsWidgetBase massages the form values - which might be just a string for the entire widget (i.e. all deltas) - into the proper array structure in a #element_validate callback. This leads to ::massageFormValues() being called with a string as the first argument which fatals (because the #after_build callback leads to ::massageFormValues() being called prior to validation. List logic cannot be moved into a #value_callback because then FormValidator::performRequiredValidation would complain that the value (which would be an array then) is illegal.
πŸ“Œ Task
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

entity system

Created by

πŸ‡©πŸ‡ͺGermany tstoeckler Essen, Germany

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank you for creating this issue to improve Drupal.

    We are working to decide if this task is still relevant to a currently supported version of Drupal. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or is no longer relevant. Your thoughts on this will allow a decision to be made.

    Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

Production build 0.71.5 2024