Problem/Motivation

Sometimes, we need a way to override a form_mode based on some conditions. The common use case is to apply different form modes for different user roles.
The workaround was possible via hook_entity_form_display_alter hook with passing along $entity. That was the original idea of this issue.

Proposed resolution

The initial resolution was to add entity object to the $context array.

However, in #14 @ amateescu came up with a better idea:

It would be better to introduce a new hook: hook_entity_form_mode_alter(), to mirror the one we have for altering view modes: hook_entity_view_mode_alter().

So let's do this!

Remaining tasks

Review MR - https://git.drupalcode.org/project/drupal/-/merge_requests/188
Review CR - https://www.drupal.org/node/3189884
Commit :)

User interface changes

-

API changes

As mentioned in the CR, there is a new hook hook_entity_form_mode_alter, that allows to alter form_mode for given entity.

Issue fork drupal-2899847

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Johnny vd Laar created an issue. See original summary.

Johnny vd Laar’s picture

Status: Active » Needs review
StatusFileSize
new619 bytes

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

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.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.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.

cameron prince’s picture

Status: Needs review » Reviewed & tested by the community

+1 for this... I've tested the patch and it works as described.

This is a critical need for fully exploiting form modes.

As non-intrusive as this change is, would tests be required?

alexpott’s picture

Version: 8.6.x-dev » 8.7.x-dev
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests, +Needs change record

Thanks for working on this.

In order to commit a new feature we need an automated to test to prove that we've added what we think we have and ensure that we don't break it in the future. For more information about writing tests in Drupal 8 see the following links:

  1. https://www.drupal.org/docs/8/testing
  2. https://api.drupal.org/api/drupal/core%21core.api.php/group/testing/8.7.x

Also this is a change so developers need to be informed about the change so a change record is required. Plus we need to update the documentation of hook_entity_form_display_alter() in core/lib/Drupal/Core/Entity/entity.api.php

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.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.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.

Matroskeen made their first commit to this issue’s fork.

matroskeen’s picture

Title: Pass the entity to hook_entity_form_display_alter such that you can alter the form_mode based on a property of the entity » Add entity object to hook_entity_form_display_alter $context array
Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs tests, -Needs change record

Here is a list of applied actions:
1) The merge request was opened with changes from #2, updated documentation of hook_entity_form_display_alter() and adding a reference to $context['entity'] in test. I was wondering if we need some extra test coverage if we already have testEntityFormDisplayAlter().
2) IS was updated according to the template;
3) Draft change record was created: https://www.drupal.org/node/3189884 (this is my first change record, so I'd like someone to review)

amateescu’s picture

Status: Needs review » Needs work
Issue tags: -Entity forms, -Display modes
StatusFileSize
new750 bytes

I don't think hook_entity_form_display_alter() is the right place to change the form display, it happens a bit too late in the "render display collection process", most importantly, after we possibly create a fresh runtime object if a form display for the given form mode doesn't exist.

It would be better to introduce a new hook: hook_entity_form_mode_alter(), to mirror the one we have for altering view modes: hook_entity_view_mode_alter().

Attaching a patch with the place where I think it should be invoked.

amateescu’s picture

matroskeen’s picture

Title: Add entity object to hook_entity_form_display_alter $context array » Add hook_entity_form_mode_alter hook to allow altering form_mode
Issue summary: View changes
Status: Needs work » Needs review

@amateescu, thank you for your input, it makes perfect sense. I've updated the MR, you're welcome to review it again.

I removed the $context because it seems redundant. I also noticed that $context is always empty in hook_entity_view_mode_alter and created a separate issue - #3193131: $context in hook_entity_view_mode_alter is always empty.

Also, changed the issue title and description to align it with the proposed resolution and merge request.

amateescu’s picture

Status: Needs review » Reviewed & tested by the community

Looks great to me! Thanks for following up with it, @Matroskeen :)

  • catch committed f049151 on 9.2.x
    Issue #2899847 by Matroskeen, amateescu, Johnny vd Laar, alexpott: Add...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Looks great! Committed f049151 and pushed to 9.2.x. Thanks!

Status: Fixed » Closed (fixed)

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

donquixote’s picture

The initial resolution was to add entity object to the $context array.

However, in #14 @ amateescu came up with a better idea:

It would be better to introduce a new hook: hook_entity_form_mode_alter(), to mirror the one we have for altering view modes: hook_entity_view_mode_alter().

I like the "better idea".
However, the original idea was also good, and I don't see them as mutually exclusive.
Perhaps we can open a follow-up issue?

In my own use case, I want to change the widget type for a base field depending on the current value stored in the entity.
I think this can be done in other ways (creating a custom widget type), but it still makes sense to me to expose this in the hook.