WorkspacesHtmlEntityFormController builds entity forms twice

Created on 17 April 2025, about 2 months ago

Problem/Motivation

\Drupal\workspaces\Controller\WorkspacesHtmlEntityFormController::getContentResult() calls ::getFormObject() once at the beginning of the method to retrieve the entity from the form object, and again at the end through the call to ::getContentResult() on the inner service.

Steps to reproduce

- add a breakpoint in a hook_entity_create() implementation and load the entity add page for a workspace-supported entity type
- notice that the breakpoint gets hit twice

Proposed resolution

Retrieve the entity "manually" from the route match object.

Remaining tasks

Review.

User interface changes

Nope.

Introduced terminology

None.

API changes

Nope.

Data model changes

Nope.

Release notes snippet

Nope.

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component

workspaces.module

Created by

๐Ÿ‡ท๐Ÿ‡ดRomania amateescu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @amateescu
  • Merge request !11873Stop building entity forms twice. โ†’ (Closed) created by amateescu
  • Pipeline finished with Failed
    about 2 months ago
    Total: 123s
    #476090
  • Pipeline finished with Failed
    about 2 months ago
    Total: 689s
    #476093
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Seems like a good refactor. Leaning on the test coverage for this one

  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    The test coverage should use key/value directly rather than state.

    But also - would it be unreasonable to copy FormController::getContentResult() into this method to avoid the double retrieval? It's about ten lines of code.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia himani_219

    I agree with the core concernโ€”calling ::getFormObject() twice is inefficient and can lead to unexpected behavior like duplicate hook_entity_create() triggers. The proposed fix to retrieve the entity manually from the route match object is a clean and straightforward improvement.

    Regarding @catch's suggestion: copying the logic from FormController::getContentResult() into WorkspacesHtmlEntityFormController seems reasonable here, especially since it avoids unnecessary indirection and the performance hit of redundant instantiation. It's only a few lines and would help make the control flow clearer and more predictable in this workspace-specific context.

  • ๐Ÿ‡ท๐Ÿ‡ดRomania amateescu

    Updated the test to use key-value instead of state.

    @catch, copying FormController::getContentResult() wouldn't help us in any way, it's HtmlEntityFormController::getFormObject() that builds the form, and the MR is actually doing what you're suggesting, it copies the relevant parts from it into WorkspacesHtmlEntityFormController::getContentResult() :)

  • Pipeline finished with Failed
    28 days ago
    Total: 507s
    #492474
  • Pipeline finished with Success
    27 days ago
    Total: 441s
    #493389
    • catch โ†’ committed 81fee3b9 on 11.2.x
      Issue #3519766 by amateescu: WorkspacesHtmlEntityFormController builds...
    • catch โ†’ committed 18601506 on 11.x
      Issue #3519766 by amateescu: WorkspacesHtmlEntityFormController builds...
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom catch

    OK that all makes sense. Committed/pushed to 11.x and cherry-picked to 11.2.x, thanks!

Production build 0.71.5 2024