Created
October 9, 2015 12:49
-
-
Save Luukyb/7bbf48c30660da547b6b to your computer and use it in GitHub Desktop.
Tiny Drupal 8 module to hide a few fields for non admin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't get this code to work