Problem/Motivation

The main premise of entity forms is that we get to work with an entity object at all times instead of checking submitted values from the form state. However, this premise is currently broken in the main form() method when the form is rebuilt via AJAX.

Proposed resolution

Add an #after_build callback that updates the entity object with user-submitted values from the form state.

It seems that content entity forms really need the current flow, probably because field api has its own #after_build callback for widgets, so let's make it specific to them. This allows us to fix config entity forms which use only raw Form API code.

Looks a bit surprising when seen on its own. Would help seeing the big picture if the comment made it clearer that we're undoing part of what the parent did. Also maybe by removing the empty line and grouping the code more ?

Comments could be enhanced a bit IMO. Proposals below are what I feel would have helped me grasp the logic faster :-)

- Phpdoc : "This is the entity object builder function that allows..." : slightly confusing, an #after_build callback is not an entity builder function.
Proposal :
"Updates the internal $this->entity object with submitted values when the form is being rebuilt (e.g. submitted via AJAX), so that subsequent processing (e.g. AJAX callbacks) can rely on it." ?

- Inner code comment : the logic around "detect if we're doing the initial build or a rebuild" could be more explicit.
Proposal : "Rebuild the entity If #after_build" is being called as part of a form rebuild, i.e. if we are processing input.".