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(&$variables, $hook) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$variables['view_mode_2'] = $node->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.