Problem/Motivation

The field_attach_form hooks were removed in #2095195: Remove deprecated field_attach_form_*(), but by removing this, it also removed the possibility to alter entity forms.

An example:
There is currently an issue in field_group when paragraphs are added on the node form. #2637148: Field Group not working on node edit inside paragraph
What is happening: Because of the lack of hooks, field_groups tried to add the grouping via a regular form_alter. This works perfect for node forms, but is causing issues when multiple entity forms are embed into 1 form (paragraphs, inline entity form, field_collection, ...). Field_group has no clue that multiple entity forms will be shown, and on what places this will be. (unless we loop recursive trough the whole form, but even then we probably have missing info).

Proposed resolution

Introduce an alter hook in EntityFormDisplay:buildForm. This will provide other modules the option to hook into the building of an entity form. Even when multiple entity forms are build into 1 form.

User interface changes

None

API changes

A new hook is introduced

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

zuuperman created an issue. See original summary.

nils.destoop’s picture

Status: Active » Needs review
FileSize
682 bytes

An example patch, but probably a better name should be found.
But i tested the hook with field_group, and all issues are gone then. I can add group info for my node, and for my paragraph. The form is rendered correctly.

nils.destoop’s picture

Issue summary: View changes
swentel’s picture

Issue tags: +Needs tests

Hmm, interesting. EntityViewDisplay has an calling entity_display_build (looking at it, I even wonder if that hook actually works since $key seems totally wrong there, but that's for another issue) - maybe it could be something like entity_form_display_build then ?

Another option maybe: what if move this alter call into EntityFormBuilder::getForm, in analogy with EntityViewBuilder and name it something like 'entity_form'

e.g.

  $this->moduleHandler()->alter(array('entity_form'), $build_list[$key], $entity, $display);

Definitely needs tests and an entry in entity.api.php

nils.destoop’s picture

Moving it to getForm will probably lead to the same issue like we have now.
This is how paragraphs is embedding the entity form: http://cgit.drupalcode.org/paragraphs/tree/src/Plugin/Field/FieldWidget/...

swentel’s picture

Ha, interesting .. form in a form, always tricky stuff :)

Been looking at how Entity Inline Form deals with this and they are calling an alter themselves on the embedded form (see EntityInlineForm::entityForm()). Maybe in the end, that's the that's a better idea for paragraphs module as well ?

(I'm even wondering whether they shouldn't start depending on IEF, less code to maintain as well, but that's separate issue for them)

Petr Illek’s picture

Hi,
it is safe to use the patch from #2 at this moment as temporary solution?
Or it is better to wait for some real solution?

Thanks a lot for your work so far.

geoffreyr’s picture

Would this be the reason why it's currently impossible to use Field Groups to modify the form layout of Contact Forms?

swentel’s picture

@Petr Illek no, don't use this patch for now. I don't know how @zuuperman tackled it in the end, but we talked in IRC and we were close to setting this to won't fix.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Revathi Manohar’s picture

Status: Needs review » Closed (works as designed)

hi,
i am also tested with the field group and paragraph demo modules. image + text are grouped.

thank you

Revathi Manohar’s picture

hi,Only local images are allowed.
i am also tested with the field group and the paragraph modules.image and paragraph are grouped and working fine,if it have any further issue kindly post.according to patch it is working fine.

reekris’s picture

Status: Closed (works as designed) » Needs review

@revathi seems like you are posting about another issue? This issue is about getting field groups to work when editing content and your screenshot is showing a viewed node. I'm setting this back to previous status since I don't believe it is actually fixed.

swentel’s picture

swentel’s picture

Status: Needs review » Needs work

Closed #2691723: Add a hook to alter entity form displays - moving to needs work for tests.

jwilson3’s picture

Copying Comment #4 from dawenher here from the other issue that explains what tests are required:

For hook_entity_display_build_alter() we have core/modules/field/tests/src/Kernel/DisplayApiTest.php:156 currently, so maybe opening up a FormDisplayTest in there would be the right way. ON top of that then add something like \field_test_entity_display_build_alter for your new hook maybe.

Note: We also have to add documentation to entity.api.php

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

artis’s picture

Status: Needs work » Fixed
Issue tags: -Needs tests

This has been fixed elsewhere it seems. Feel free to re-open if I'm wrong.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

nedjo’s picture

@artis

Anything you can point or link to that makes it seem like this is fixed? Thanks.

plach’s picture

Title: Contrib modules can't alter the building of entity forms » Contrib modules can't alter the building of entity form displays
Status: Closed (fixed) » Needs work
Issue tags: +Needs tests

This doesn't seem to be fixed at all_ there is still no hook allowing to alter the form display building process AFAICT.

geek-merlin’s picture

Another one rides the bus! This bites in that related issue.

geek-merlin’s picture

So let's sort it out:
* we have a patch in that dup issue #2691723-2: Add a hook to alter entity form displays that adds a simple hook but needs work for tests
* EDIT: also that patch needs doc (.api.php) for the introduced hook_entity_form_build

Note: I'm not sure if the hook is enough as sometimes modules need to know if they're in the top level entity form or an embedded entity form. But i'd leave this for a followup if needed.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

pcambra’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Needs work » Needs review
FileSize
1.68 KB

Rerolled and added the api docs, tests still missing. Worth mentioning that I think most of the use cases related to this could maybe solved by hook_field_access or a good old hook_form_alter.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

longwave’s picture

Category: Bug report » Feature request
Status: Needs review » Postponed (maintainer needs more info)
Issue tags: +Bug Smash Initiative

This issue came up in #bugsmash triage.

Myself, @lendude and @larowlan discussed it and agreed that it is not a bug and would be better classified as a feature request. The discussion above indicates that paragraphs and field_group have already worked around this, and there has been little recent interest in fixing this issue. Setting to "maintainer needs more info", if there is no further movement for this feature request in the future then it is likely to be closed as won't fix.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (won't fix)

Per #34, since it's been a year without movement going to go ahead and close.

If anyone disagrees please reopen expressing why though.

Thanks!

Liam Morland’s picture

The code still contains this comment in two places:

// @todo Remove as part of https://www.drupal.org/node/2640056

If this won't be fixed, perhaps this comment should be removed.