Skip to content

Instantly share code, notes, and snippets.

@Luukyb
Created October 9, 2015 12:49
Show Gist options
  • Save Luukyb/7bbf48c30660da547b6b to your computer and use it in GitHub Desktop.
Save Luukyb/7bbf48c30660da547b6b to your computer and use it in GitHub Desktop.
Tiny Drupal 8 module to hide a few fields for non admin.
<?php
/**
* @file
* Small module to hides a few fields for non admin.
*/
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_entity_field_access().
*/
function my_module_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account, \Drupal\Core\Field\FieldItemListInterface $items = NULL) {
$fields_names = array(
'field_name',
'field_surname'
);
if (in_array($field_definition->getName(), $fields_names)) {
$account = \Drupal::currentUser();
if (!in_array('admin', $account->getRoles())) {
return AccessResult::forbidden();
}
}
return AccessResult::neutral();
}
@law-dono
Copy link

I can't get this code to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment