Getting node view mode in field preprocess I am trying to get the view mode of a node to use it in a field twig file by writing this function in my_theme, but it doesn't work:

function MY_THEME_preprocess_field(&amp;$variables, $hook) {
 if ($node = \Drupal::routeMatch()-&gt;getParameter('node')) {
  $variables['view_mode_2'] = $node-&gt;view_mode;
 }
}


I don't know if the variable is correct, but when I write some echo code I see that already the if condition isn't fulfilled.

EDIT: the if condition is only fulfilled when the node is open, but not when its teaser is shown as part of a view on the front page.
In a field template the view mode is stored here:

$variables['element']['#view_mode']


So you could check in preprocess, if the node is not displayed as teaser: 

function MY_THEME_preprocess_field(&amp;$variables) {
  if ($variables['element']['#view_mode'] != 'teaser')  {
    // node is not displayed on the front page
  }
}


The same condition in a field twig:

field.html.twig

{% if element['#view_mode'] != 'teaser' %}
  node is not displayed on the front page
{% endif %}