I have an image field on Basic Page content type with a View Mode where format = Image and Image Style = Large.
How can I write a template_preprocess_node hook to get the Image style that is based on the View mode?
Here is what I was thinking but got stuck and cannot figure out what to call.
function mymodule_preprocess_node(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$node = $variables['elements']['#node'];
if($variables['view_mode'] = 'default' && $node->hasField('field_image')){
$fieldImage = $node->get('field_image');
/** stuck here as I cannot find method to get the Image Style **/
//goal
$image_style = $fieldImage->getImageStyle() // not sure what method this would be
$image_path = $fieldImage->entity->getFileUri();
$image_url = ImageStyle::load($image_style)->buildUrl($image_path);
$variables['content'][0]['image_url'] = $image_url; // this is the goal
}
}
twig template
<div style="background-image: url('{{ content.image_url }}')"></div>