How do you get the currently chosen image style name from the field format display option?
I can load an image with a given style with the code below, however, the style name (e.g. thumbnail) is hard-coded.
$imageStyle = \Drupal::entityTypeManager()
->getStorage('image_style')
->load('thumbnail');
I am hoping to get the selected style name dynamically.
MYMODULE.module:
HOOK_preprocess_menu() {
$imagePath = getUser()['currentUser']->$imageFieldName
->entity->uri->value;
$imageStyleFormatterValue = ???;
$imageStyle = \Drupal::entityTypeManager()
->getStorage('image_style')
->load($imageStyleFormatterValue);
$imageStyleURL = $imageStyle
->buildUrl($imagePath);
$markup = new TranslatableMarkup(
'<span style="background-image: url(@imageStyleURL);"></span>', [
'@imageStyleURL' => $imageStyleURL,
]
);
}
For my case, $imageStyleURL
will be printed as background-image: url();
, so I need to build the path in preprocess.
I've tried going through FieldConfig
, ->entity
, and FieldStorage
, but cannot find the image style value stored anywhere.