How to change image style using hook_views_pre_render() I am trying to adapt the following Drupal 7 code to Drupal 8, in order to set a different image style to every image in a view:

// Assigns a random style name to each image
function &lt;MODULE_NAME&gt;_views_pre_render(&amp;$view)
{    
  if($view-&gt;name == 'SOME VIEW') 
  {
    $styles = array('thumbnail', 'medium', 'large');
    foreach($view-&gt;result as &amp;$result) 
    { 
      $result-&gt;field_field_image[0]['rendered']['#image_style'] = $styles[array_rand($styles)];        
    } 
  }
}


Right now I have:

function &lt;MODULE_NAME&gt;_views_pre_render(ViewExecutable $view)
{    
  if($view-&gt;id() == 'SOME VIEW') 
  {
    $styles = array('thumbnail', 'medium', 'large');
    foreach($view-&gt;result as &amp;$result) 
    { 
      $image = $result-&gt;_entity-&gt;get("field_media_image");
      $image-&gt;SOME_FUNCTION('#style_name') = $styles[array_rand($styles)];        
    } 
  }
}


However, I can't figure out what SOME_FUNCTION would be -- There does not appear to be any way to access the render array from an ImageItem object.
I gave up on hook_views_pre_render and wound up meeting my requirements with template_preprocess_image_formatter as follows:

function &lt;TEMPLATE_NAME&gt;_preprocess_image_formatter(&amp;$variables) {
  $styles = array('thumbnail', 'medium', 'large');
  if ($variables['image']['#style_name'] === 'medium') {
    $variables['image']['#style_name'] = $styles[array_rand($styles)];
  }
}