Problem/Motivation
My use-case:
An anonymous user can create a membership node. That form has many details that our client uses to determine if they want to approve od decline the membership.
Once the membership is approved the original author can then edit their own membership node but without all the registration details.
I want to control the displayed form by creating multiple form modes and then access the mode based on the role of the user trying to edit the node.
I know this can be achieved with field permission hooks but since drupal already offers the ability to create multiple form modes i just wanted to find a simple way to access them. Different form modes also allow you to have easy control over field groups and field widgets, if you want them differently per form mode.
Here is how we have been using it for a couple of weeks now without problems.
If you got to /admin/structure/display-modes/form and create an additional form mode for content you can then use this hook to access that form mode.
for example:
function MODULE_entity_form_mode_alter(&$form_mode, $entity) {
$entity_type = $entity->getEntityTypeId();
if ($entity_type === 'node' && $entity->getType() === 'membership') {
$user = \Drupal::currentUser()->getAccount();
if ($user->isAuthenticated()) {
$form_mode = 'member'; // This is where we set the newly generated form mode.
}
}
// Show the user form for admin in the form mode of 'staff_only'.
elseif ($entity_type === 'user') {
$user = \Drupal::currentUser()->getAccount();
if (_is_user_an_admin($user)) { // Custom function that checks if user has admin roles.
$form_mode = 'staff_only';
}
}
}
Proposed resolution
Write a hook that allows you to create custom conditions to alter the form mode before the form is loaded.
Remaining tasks
- Discuss if this is a good way to go
API changes
A new hook is added.
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | 2795795-form_mode-9.patch | 4.1 KB | tim.plunkett |
| #2 | 2795795_EntityFormModeAlter.patch | 698 bytes | MaskOta |
Comments
Comment #2
MaskOta commentedMy stab at it.
Comment #3
primsi commentedComment #4
primsi commentedCan you update the issue summary with a use case for this?
Comment #5
joachim commentedI'm also confused about the use case.
Though https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityForm... doesn't seem to let you pick a form view mode.
Comment #6
MaskOta commentedI have added our use-case to the summary.
Comment #7
tim.plunkettIIRC the $operation is also assumed to be the view mode.
\Drupal\Core\Entity\ContentEntityForm::init() calls
$form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation());\Drupal\Core\Entity\Entity\EntityFormDisplay::collectRenderDisplay()is defined aspublic static function collectRenderDisplay(FieldableEntityInterface $entity, $form_mode) {Comment #8
joachim commentedYeah, it does seem that way.
But isn't that a little weird? What happens if I create a form view mode called 'delete'?
But anyway, if that's the case, can the form view mode maybe be changed by altering the route?
Comment #9
tim.plunkettYou can change the operation via a route alter, but that would also change which form is loaded. Less than ideal.
If we could break BC by changing EntityFormInterface, I would do this (see patch).
Then again, I highly doubt anyone is implementing EntityFormInterface without extending EntityForm, given the setter injection approach (see the weirdly specific methods on there like setStringTranslation, setModuleHandler, setEntityManager, and setEntityTypeManager).
Comment #10
MaskOta commentedLet me try to be a little more clear in what i am trying to do. (updated summary)
An anonymous user can create a membership node. That form has many details that our client uses to determine if they want to approve od decline the membership.
Once the membership is approved the original author can then edit their own membership node but without all the registration details.
I wanted to control that by creating multiple form modes and then access the mode based on the role of the user trying to edit the node.
I know this can be achieved with field permission hooks but since drupal already offers the ability to create multiple form modes i just wanted to find a simple way to access them. Different form modes also allow you to have easy control over field groups and field widgets, if you want them differently per form mode.
Does this make more sense? If i understand the patch by tim.plunkett, that allows you to access a form mode via a different URL?
Comment #12
woprrr commentedYou can use all form-modes you needed with help of FormModeManager module. That permit you to use form-modes on each entities compatible with this and have out of the box an ui to use it totally intagrated with core UI.
Comment #23
smustgrave commentedThank you for sharing your idea for improving Drupal.
We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. 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!
Comment #25
smustgrave commentedAlso not super clear here but since it's been 10 years and a feature request without any movement or follow up is it really a desired feature for core?