5

What I need to do

I have some user account fields that should be administrator-only (read/write/edit). For example, the IP address used to register.

So, I want to restrict access to administrators.

Current state of field permissions in Drupal 8 (as of Feb 19, 2016)

8

You're stuck doing this in code, but the Examples module includes a field_permissions example module.

  • My use case was to modify visibility of one of two fields in a Paragraph. But in the edit form I needed to show it as an item and not an editable field. Which is beyond the scope of the Field permissions module. So thanks for pointing out the examples. – Japo Domingo Jul 5 '17 at 2:00
3

This is an old thread, but as I stumbled upon same problem, not wanting to use a heavy module for few fields, I ran into this article (after this one, so I thought I could share) : https://atendesigngroup.com/blog/form-and-view-modes-vs-field-access-drupal-8

From other exemples around, this one takes care of cache context .

Just be careful to comments below article and some other little mistakes in code example. Correct and working code looks like this :

function YOUR_MODULE_NAME_entity_field_access($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) {
  $result = AccessResult::neutral();
  if ($field_definition->getName() == 'field_we_care_about') {
    if ($operation == 'edit' && !in_array('administrator', $account->getRoles())) {
      $result = AccessResult::forbidden();
    }
  }
  return $result->addCacheContexts(['user.roles:administrator']);
}
2

Field Permissions has a beta release for D8 now. It allows you to set permissions per field.

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.