3

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.

2
  • 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
    Commented Dec 10, 2015 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
    Commented May 26, 2017 at 10:06

5 Answers 5

2

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.

4
  • 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
    Commented Dec 10, 2015 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
    Commented Dec 11, 2015 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
    Commented Jan 1, 2019 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
    Commented Jan 2, 2019 at 16:37
2

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.
0
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...

1

It is also fairly easy to utilize this in a custom hook (inspired by Form Mode Control module): hook_entity_form_display_alter is your friend.

my_module.module


// &$form_display is importent here, as the whole object is swapped out, not just modified
function my_module_entity_form_display_alter(EntityFormDisplayInterface &$form_display, array $context) {

  if (in_array('my_role', \Drupal::currentUser()->getRoles()) && $context['entity_type'] === 'node' && $context['bundle'] === 'my_node_type') {

    $storage = \Drupal::entityTypeManager()->getStorage('entity_form_display');
    $differentDisplay = $storage->load("{$context['entity_type']}.{$context['bundle']}.my_form_display"); // 'my_form_display' was created via /admin/structure/display-modes/form 

    if ($differentDisplay) {
      $form_display = $differentDisplay;
    }
  }
}

The above example will display my_node_type form in the display 'my_form_display' for users with the role 'my_role'.

0

There is a new module which gives us such functionality https://www.drupal.org/project/role.

Base module features:

Control user edit form mode per Role

Control user full view per Role

Sub modules:

Role Appearance: Control site theme per user Role Role registration: add a new route 'user/register/{role_id}' for specific role registration Add a new plugin type RoleConfigElement which allows add extra fields to Role.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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