How can I hide fields by user role when viewing a node? I have managed to hide certain fields for certain roles programmatically in Drupal 8 using function module_name_form_node_form_alter(array &amp;$form, FormStateInterface &amp;$form_state, $form_id).

The matter is that what I managed to hide such fields in the "Edit" tab, but I would also like to do the same in the "View" tab. What I intend to do is to hide certain fields depending on their machine name and the role of the user logged.

Do any of you know which hook should I use for that?
OK, this code seems to be the solution:

/**
 * Implements hook_entity_field_access().
 */
function MYMODULE_entity_field_access($operation, \Drupal\Core\Field\FieldDefinitionInterface $field_definition, \Drupal\Core\Session\AccountInterface $account, \Drupal\Core\Field\FieldItemListInterface $items = NULL) {
  if ($field_definition-&gt;getName() == 'NAME_OF_THE_FIELD' &amp;&amp; $operation == 'view') {
    return \Drupal\Core\Access\AccessResult::forbidden();
  }
  else {
    return \Drupal\Core\Access\AccessResult::neutral();
  }
}