field_main_image->entity; if ($file) { $variables = array( 'responsive_image_style_id' => 'header_image', 'uri' => $file->getFileUri(), ); // The image.factory service will check if our image is valid. $image = \Drupal::service('image.factory')->get($file->getFileUri()); if ($image->isValid()) { $variables['width'] = $image->getWidth(); $variables['height'] = $image->getHeight(); } else { $variables['width'] = $variables['height'] = NULL; } $logo_build = [ '#theme' => 'responsive_image', '#width' => $variables['width'], '#height' => $variables['height'], '#responsive_image_style_id' => $variables['responsive_image_style_id'], '#uri' => $variables['uri'], ]; // Add the file entity to the cache dependencies. // This will clear our cache when this entity updates. $renderer = \Drupal::service('renderer'); $renderer->addCacheableDependency($logo_build, $file); // Return the render array as block content. return $logo_build; } } return NULL; }