I created a simple content type and a form mode for this content type. Where can I view this form mode after creating it? I see no option for form modes when creating a content type. I would have expected an option to be presented when clicking on "Add Content" to select "Add xy content with xy form mode"?
Perhaps with a simple get var like
add/page/?form=light
add/page/?form=heavy
Comment | File | Size | Author |
---|---|---|---|
#61 | 2530086-61.patch | 2.29 KB | Prem Suthar |
#58 | 2530086-58.patch | 2.16 KB | Prem Suthar |
| |||
#57 | reroll_diff_43-57.txt | 2.47 KB | pooja saraah |
#57 | 2530086-57.patch | 2.16 KB | pooja saraah |
#43 | 2530086-form_mode-43.patch | 2.17 KB | vacho |
Issue fork drupal-2530086
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
Comment #1
giorgio79 CreditAttribution: giorgio79 commentedComment #2
yched CreditAttribution: yched commented@giorgio79 : Right, form modes can only be leveraged via custom code in custom controllers - at least at the moment nothing in core makes use of custom-defined form modes.
(unlike custom view modes, that can be selected in Views, or in entity_reference formatters - but those features have no equivalent on the form side)
The feature you mention seems like something a contrib module could provide, as it might not be a desired behavior on all sites.
Comment #3
BerdirYes. At the moment, this simply doesn't exist. https://www.drupal.org/project/issues/entityform_block is one thing that will eventually leverage it.
Comment #4
stevectorComment #5
singularoI need to create a form that has different fields present for different roles. I thought that using the new form modes would be more sensible than trying to hide fields via a form_alter as in d6/d7, and I think that I've almost worked it out. I have a node type 'custom_profile' and a new display mode 'administrator_profile_form' with all the fields visible, and on the 'default' display mode, most of the fields hidden. Then this code in a custom module to get the new display mode to show (conditional role stuff yet to be done):
Using phpstorm+xdebug, I can see that the form_state is updated, and everything looks like it should display with the new display mode but I still get the 'default' form.
Any pointers as to where I'm going wrong?
Comment #6
stevectorHi Singularo, I think you should approach this task with the mindset of asking for the form mode you want, rather than overriding/altering a form mode once the processing has started.
For instance if you are looking to change the form at node/add/page then look at the controller for that path.
http://cgit.drupalcode.org/drupal/tree/core/modules/node/src/Controller/...
The relevant chunk of code is
$form = $this->entityFormBuilder()->getForm($node);
I think you'd want to override that method/route and replace it with something like
Also look at #2511720: Allow form modes to use default operation if a form operation is not explicitly set to make sure the form mode does not throw an error when it is called.
Comment #7
BerdirShowing/Hiding fields but keep them otherwise the same is IMHO a perfect use case for hook_entity_field_access() not switching out the form display. I'd only try to do something like that when you want to show different widgets based on the user roles.
Comment #8
stevectorGood point Berdir. I agree. Field access is a better fit if per-field visibility by role is the only difference between the forms.
Field Permissions module module has handled that use case well. There is not yet a Drupal 8 version: #2196303: Port field permissions to drupal 8
Comment #9
rattusrattus CreditAttribution: rattusrattus at CTI Digital commentedRe #6 I do not believe that the second argument to the EntityFormBuilder getForm method is the form mode; this is the operation and, from what I can tell from the interface doc block, it concerns the crud operation rather than the form mode. When passing the my form mode key to getForm I get the following error:
Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException: The "node" entity type did not specify a "admin" form class. in Drupal\Core\Entity\EntityTypeManager->getFormObject() (line 182 of /var/www/drupal/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php).
So I'm still looking for a way to render an entity form with a particular form mode...
Any pointers greatly appreciated.
Comment #10
jacov CreditAttribution: jacov as a volunteer commentedi too was looking for something like:
add/page/?form=light
add/page/?form=$form_mode
imho this should be native to the functionality of the core module and should not depend on entityform_block
Comment #11
farald CreditAttribution: farald commentedI agree with jacov. What I would want to do, is have a separate form configured to do a separate task.
Scenario: You have a node which two roles should have access to.
1: Content proposer: /node/add/content-> Add title and node field content only.
2: Content promoter: /node/X/promotion -> Edit the same node, have no access to fields above, but two other fields, "Promote" checkbox and "Promotions comments" field.
A scenario like this should "easily" be handled natively through UI.
Comment #12
robertwb CreditAttribution: robertwb commentedIMO a way to access alternate form modes via URL. Rather than GET params, I think they should be added as routes, admins should be able to enable a route in admin/structure/display-modes/form/manage/[display mode machine name] (disabled by default to reflect @yched concern in #2 ).
Comment #13
dench0 CreditAttribution: dench0 commented1. create needed form modes, for example I created for content custom form mode - "my_mode" (machine name node.my_mode )
2. implement hook:
3. build form ($node - is loaded node object):
Comment #15
DuneBL@dench0 (#13)
I couldn't understand where to put the chunk of code defined in "3. build form"
Comment #16
eloivaqueMeanwhile you can try this modules.
https://www.drupal.org/project/form_mode_control
https://www.drupal.org/project/form_mode_manager
I prefer form_mode_control. Form_mode_manager create view form mode for all content types.
Comment #18
taggartj CreditAttribution: taggartj as a volunteer commentedHello Well after some time of messing with this i seem to have it working:
and here is how
1) create your form display and get id from url
at :admin/structure/display-modes/form/manage/
"Test This" gives m name ... node.test_this
in your modules
my_module.routing.yml
.... now in your module
my_module.module
now you can go to the path www.site.com/some-path/{nodeID}/test_this
and try to not have your chin hit the floor.
the cool thing as it gives a new form id to then alter
Comment #19
chriscalip CreditAttribution: chriscalip commentedThis was a PITA! For this feature to be operational for nodes.
Add this to your module.
This #19 has bugs and as well as #18. For custom forms use #13 instead.
Comment #20
grumpy74 CreditAttribution: grumpy74 commented#18 works perfectly
With some improvments like adding tasks tabs as on usual node form pages and adding some custom permissions, it's easy to create a per permission access to different form display.
Thanks a lot taggartj.
I'm working on a dynamic way to generate form modes routes and permissions (like "form mode manager" and "form display control" modules should do, but both are broken in Drupal 8.2.3 version)..
UPDATE : aftr testing the module "form mode manager" works perfectly.
Comment #21
SteffenR@chriscalip: Thanks for posting a working solution - works fine in my case.
Comment #23
tim.plunkettJust a proof of concept, and this has backwards compatibility issues, but this would allow custom code to use form modes correctly AFAICS
Comment #24
Pavan B S CreditAttribution: Pavan B S at Valuebound commentedRe rolled the patch, please review.
Comment #25
MaskOta CreditAttribution: MaskOta commentedComment #26
NewZeal CreditAttribution: NewZeal as a volunteer commentedAnother way to solve this problem is as follows:
Replace items in caps with own values.
Comment #27
woprrr CreditAttribution: woprrr as a volunteer and at NeoLynk commentedYou can use FormModeManager module too for use form-mode onto ALL entities compatibles + media/file entity (and your custom).
You can too inspirate you with our method to make you custom implement or with help of FormModeManager services.
Comment #28
kalidasan CreditAttribution: kalidasan commentedTried
hook_entity_type_alter, hook_entity_type_build
but no luck.Ref: https://www.webomelette.com/render-custom-entity-form-modes-programatica... (Not worked)
But #26 is working fine after spending so much time.
Link to add form modes :
admin/structure/display-modes/form
Thanks @New Zeal for your solution :)
Comment #30
joachim CreditAttribution: joachim at Torchbox commentedRather than adding a parameter, what about having the mode in $form_state_additions, so this works:
$form = \Drupal::service('entity.form_builder')->getForm($entity, 'edit', ['form_display' => 'my_mode']);
At the moment, it gets clobbered by ContentEntityForm::init()'s call to setFormDisplay()
Comment #31
giorgio79 CreditAttribution: giorgio79 commentedComment #32
woprrr CreditAttribution: woprrr as a volunteer and at NeoLynk commentedSo I IMHO we need to planned a port of form mode manager features/usages in core (in display reposittory). If core team is interested I purpose prépare that and see with field/core team and make an experimental module instead of continue to maintain form mode manager.
Comment #33
julius95 CreditAttribution: julius95 commented@woprr This definitely makes sense! It is strange that you have to write a module just to link to a form mode,
Comment #35
sukh.singh CreditAttribution: sukh.singh as a volunteer commentedI tried form_mode_manager as suggested by many over here but didn't work for profiles. In Readme.txt they written specifically that
I also tried
, but they also didn't work. Lastly I tried #26 solutions and it worked for me.
Thanks ton New Zeal for providing this solutions.
Comment #36
woprrr CreditAttribution: woprrr as a volunteer and at NeoLynk commentedHi sukh with 2.x branch and issue listed you can use fmm with profiles.
Comment #37
taggartj CreditAttribution: taggartj as a volunteer commentedhttps://www.drupal.org/project/form_mode_routing
can be a quick solution as well
Comment #38
yseki CreditAttribution: yseki as a volunteer and commentedBusiness Rules module now allows changing the form view mode based on any condition.
Let me know how it works for you.
Comment #40
kenton.r CreditAttribution: kenton.r commentedI have used Business Rules to switch form modes with conditions and it works. I also like the proposed direction that the Form_mode_routing module is going too. But to see a working solution based on routing and permissions in core would be good.
Comment #41
woprrr CreditAttribution: woprrr as a volunteer and at NeoLynk commented@kenton.r you will love form mode manager then ;) lot of old user of br and form mode routing have migrated here.
Actually we need a standard in routing, form mode manager try to purpose that and work hard to provide a solution that match whit all usages. Actually we can be compatible with every entities and provide also lot of feature like br.
Comment #42
vacho CreditAttribution: vacho at Skilld commentedComment #43
vacho CreditAttribution: vacho at Skilld commentedPatch rerolled
Comment #44
vacho CreditAttribution: vacho at Skilld commentedComment #48
pcambraNot sure if mentioned already but this is what Commerce uses (thanks @facine for the tip)
Btw seems core uses collectRenderDisplay with both form mode and operation because there's a fallback, it could be standardized a little bit?
Comment #53
ady1503 CreditAttribution: ady1503 as a volunteer commentedHello. In the end, is there the option that the control of the modes works? functional example? Thanks.
Comment #55
smustgrave CreditAttribution: smustgrave at Mobomo commentedThis could use an issue summary update about the proposed solution.
Comment #56
smustgrave CreditAttribution: smustgrave at Mobomo commentedShould of moved it to NW 2 months ago sorry.
Comment #57
pooja saraah CreditAttribution: pooja saraah at Srijan | A Material+ Company for Drupal India Association commentedFixed failed commands on #43
Attached patch against Drupal 10.1.x
Attached reroll patch
Comment #58
Prem Suthar CreditAttribution: Prem Suthar at Srijan | A Material+ Company for Drupal India Association commentedFixed The Custom CMD Failed Patch Of #57
Comment #59
Prem Suthar CreditAttribution: Prem Suthar at Srijan | A Material+ Company for Drupal India Association commentedComment #60
joachim CreditAttribution: joachim commentedNew parameters on both functions need to be documented!!!!!!!
Also, why change the name of $entity_type_id?
Comment #61
Prem Suthar CreditAttribution: Prem Suthar at Srijan | A Material+ Company for Drupal India Association commentedReroll the Patch as per Suggestion of #60.
Comment #62
Prem Suthar CreditAttribution: Prem Suthar at Srijan | A Material+ Company for Drupal India Association commentedComment #63
smustgrave CreditAttribution: smustgrave at Mobomo commented@Prem Suthar please read the comments and tags.
This was tagged for IS update and a change record and question in #60
Also upload an interdiff with patches please
Comment #65
earthangelconsulting CreditAttribution: earthangelconsulting commentedi just want to add some confirmation, for the benefit of those who have found this page while trying to use form modes from custom code (rather than anything to do with actual code changes to Core)
those modules form_mode_control and form_mode_manager may be very handy, but if you are just trying to generate a node form from php, with an alternate (but already defined) form mode for that node type, then ALL you need is what is mentioned in #13 ! thanks, @dench0 that was very helpful!
i tried this in Drupal 9.4.15 and it still works just fine :-)
Comment #66
ady1503 CreditAttribution: ady1503 as a volunteer commented@earthangelconsulting
Can you show an real example?
Where did did you implement the code.
Thanks
Gracias
Comment #67
earthangelconsulting CreditAttribution: earthangelconsulting commentedfoo_posting is the machine name of a node type
foo_specialform is the machine name of a custom form mode, for nodes of type foo_posting
foomodule is the module that handles this
this is in the src/FooController.php script, within foomodule: (note that this use case is for a path that calls a node/add form, using form mode foo_posting ... if you need to edit existing nodes, it would be a bit different)
this is in foomodule.routing.yml (nothing out of the ordinary here! )
and this is part of foomodule.module
there are definitely other ways to do this, eg: https://www.webomelette.com/how-switch-out-entity-form-mode-dynamically-... https://www.drupal.org/docs/drupal-apis/entity-api/how-to-create-an-enti... this is just one of them.
Comment #68
ady1503 CreditAttribution: ady1503 as a volunteer commented@earthangelconsulting
Thank you so much.
Gracias