2

Drupal 8 has extended the View Modes concept to Form modes

  1. Is it possible to assign a role to both "View Modes" and "Form Modes"?
  2. How does one assign a mode both view mode or form mode to be shown at a particular place ? Say I wish to Full content view mode shown at certain page and Teaser view mode show in a block at another location. Similarly I want form mode A show during create and form B during edit.

For example there is a content type with following Fields

  • Title : displayed publicly, editable by author
  • Body : displayed publicly, editable by author
  • Project code : Not displayed publicly, displayed and editable by author
  • System code : only visible/editable by admin

Essentially this could be an interesting and performant way to implement field permissions. The field permissions module has no port for Drupal 8 yet.

  • 1
    1. you would have to implements hook_entity_view_mode_alter and do role/access processing. 2. this is too brought question. For routes that are entity forms you could alter the routes and provide different view mode, but anywhere else will require direct alterations specific to the place you are altering. – user21641 Dec 10 '15 at 10:52
  • Field permissions now has a D8 port. To set the permissions up, edit the existing field and under Field visibility and permissions -> Custom permissions – powpow12 May 26 '17 at 10:06
1

Nothing in core uses form modes - it's an API waiting for contrib to make use of. This, and the question of when to manipulate form fields and when form modes, is discussed at: https://www.drupal.org/node/2530086.

There Berdir says "Showing/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 [switching form mode] when you want to show different widgets based on the user roles."

In general, managing display differently for different roles is easy, managing forms differently is more difficult.

Alternatives to the field permissions module for managing display (NOT forms):

The classic answer might be that this is a job for the Panels family, but it's mostly not quite ready for D8. Page manager is however quite usable and does much of what you want. For specifically created pages, it can control the content based on role. What it won't do is take over the rendering of all nodes of a certain content type.

Display suite is D8 ready, and allows you to show fields based on permissions by using token fields. See https://swsblog.stanford.edu/blog/using-display-suite-provide-field-level-permissions for a description of the procedure in D7, which is only cosmetically changed for D8.

  • 3
    "Nothing in core uses form modes". That's not entirely correct. user.module uses it, to differentiate between registration and user editing. That's the primary reason they exist in core. To get rid of that weird user-specific "show on register form" checkbox for fields that is in D7. – Berdir Dec 10 '15 at 20:20
  • @Berdir Is there any hope that D8 will add a UI to with hook_entity_field_access() underneath the hood? I have seen several discussions about field permissions, but they haven't gone anywhere. Field permissions module maintainers have indicated zero interest in porting to D8. This does seem like a fundamental thing that has a place in core. – amit Dec 11 '15 at 6:46
  • @amit not sure why, but I just got a notification for this comment. drupal.org/project/field_permissions has a recent RC now. – Berdir Jan 1 at 14:03
  • @Berdir thanks for the heads up. We ended up implementing what we needed above with custom code, but field permission like functionality would be nice to make it into core, perhaps D9. – amit Jan 2 at 16:37
1

Check out the Form Mode Control module.

In Drupal 8, you can create "form modes" which are for any content entity a different organization of the form (show / hidden, order, form widget, etc.).

Issue is that you can't actually use them except for register / edit profile.

This module allows you :

  • To define access to each form mode for the different roles.
  • To define for each role, each bundle of each entity which form mode should be taken by default during the creation of the content and the edition. Of course you should grant access before defining this. If a user has multiple roles, the weight of the roles is taken into account (heaviest one).
  • To access any of them if you have the permission by adding a simple parameter node/add/article?display=form_mode_machine_name.
1

After a lot of reading and pushing buttons, I was able to figure out how to add Custom "Form Mode" without using hooks or messing with API. As Berdir mention user.module is active for creating a custom form mode to display as needed, be it for user registration, profiling, etc.

What I did was "tricking" Drupal through url, like this: Go to /admin/structure/display-modes/form, where you will find that only User form modes can be added (/admin/structure/display-modes/add/user), the all you have to do is replace the /add/user part for /add/node or whatever entity type you wish.

Once you create a custom form mode display, it will "automagically" include the entity blocks you can find in Display modes, with their links and all...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.