How to find the url of a media library image field in a preprocess function? I have a preprocess function that I am using to grab one of 3 profile images.  Basically we're replacing a couple stand-alone image fields with a media library reference field.  The goal is to display the media library image if it is populated at a particular style (called '65x65') or fall back to other stand-alone legacy image fields if it is not populated.
I'm having a heck of a time building the media library image url.  I have tried quite a few things based on this discussion and this discussion and others.  The image field on the image media type is called field_media_image (screenshot).  I'm not sure if it's useful to post my half-baked code attempts so I will leave the parts that I know are working.
foreach ($block-&gt;field_block_contributors-&gt;referencedEntities() as $contributor) {
    # prefer the media library reference profile image if it exists

    if (!$contributor-&gt;field_profile_image-&gt;isEmpty()) { # check if there is a media library profile image

      $my_profile_image = $contributor-&gt;get('field_profile_image')-&gt;referencedEntities(); # seems to load this field ok
      if (!empty($my_profile_image)) {
      // kint($my_profile_image);
      # load a media library image using the '65x65' image style
      // $styled_image_url = ??????


      }
    } elseif (!$contributor-&gt;field_user_image-&gt;isEmpty()) { # or fall back to one of two legacy image fields.  This part works
      $styled_image_url = ImageStyle::load('65x65')-&gt;buildUrl($contributor-&gt;field_user_image-&gt;entity-&gt;getFileUri());
    } elseif (!$contributor-&gt;field_speaker_image-&gt;isEmpty()) {
      $styled_image_url = ImageStyle::load('65x65')-&gt;buildUrl($contributor-&gt;field_speaker_image-&gt;entity-&gt;getFileUri());
    } else {
      $styled_image_url = '';
    }
    $variables['profiles'][] = [
      'title' =&gt; $contributor-&gt;field_user_title-&gt;value,
      'link' =&gt; &quot;/user/&quot; . $contributor-&gt;uid-&gt;value,
      'image' =&gt; ['src' =&gt; $styled_image_url, 'attributes' =&gt; $contributor-&gt;field_user_image-&gt;value],
      'name' =&gt; $contributor-&gt;field_user_first_name-&gt;value . &quot; &quot; . $contributor-&gt;field_user_last_name-&gt;value,
    ];
  }

PS. Bonus beer for anyone who knows off the top of their heads how I can link to the user's name instead of their user id # ( the line    'link' =&gt; &quot;/user/&quot; . $contributor-&gt;uid-&gt;value, )
Thanks!!!
I wrote a little module I found helpful to get the media entity image values.
custom_helper_services/custom_helper_services.info.yml:
name: 'Custom Helper Services'
type: module
description: 'Provides useful services for working with themes.'
core_version_requirement: ^8 || ^9
package: 'Custom'

custom_helper_services/custom_helper_services.module:
&lt;?php

/**
 * @file
 * Contains custom_helper_services.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function custom_helper_services_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the custom_helper_services module.
    case 'help.page.custom_helper_services':
      $output = '';
      $output .= '&lt;h3&gt;' . t('About') . '&lt;/h3&gt;';
      $output .= '&lt;p&gt;' . t('Provides helpful service functions.') . '&lt;/p&gt;';
      return $output;

    default:
  }
}

custom_helper_services/custom_helper_services.services.yml:
services:
  chs:
    class: Drupal\custom_helper_services\Services\CustomHelperServices
    arguments: ['@entity_type.manager','@current_route_match','@entity.repository']

custom_helper_services/src/Services/CustomHelperServices.php:
&lt;?php

namespace Drupal\custom_helper_services\Services;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;

/**
 * Class CustomHelperServices.
 *
 * Provides helper services to be used in modules and themes.
 */
class CustomHelperServices {

  /**
   * Entity Type Manager.
   *
   * @var Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Route Match Interface.
   *
   * @var Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Entity Repository Interface.
   *
   * @var Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * CustomHelperServices constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity Type Manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
   *   The Route Match Interface.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entityRepository
   *   The Entity Repository Interface.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, RouteMatchInterface $routeMatch, EntityRepositoryInterface $entityRepository) {
    $this-&gt;entityTypeManager = $entityTypeManager;
    $this-&gt;routeMatch = $routeMatch;
    $this-&gt;entityRepository = $entityRepository;
  }

  /**
   * Build our media style path service.
   */
  public function getMediaFieldImage($mediaField = NULL, $imageStyle = NULL) {

    if ($mediaField &amp;&amp; $mediaField-&gt;entity) {

      // Get image.
      $mediaImage = $mediaField-&gt;entity
        -&gt;get('field_media_image');

      // Get image attributes.
      $alt = $mediaImage-&gt;alt;
      $width = $mediaImage-&gt;width;
      $height = $mediaImage-&gt;height;
      $title = $mediaImage-&gt;title;

      // Get image entity.
      $mediaImageEntity = $mediaImage-&gt;entity;

      // Get image mime type.
      $mediaImageMimeType = $mediaImageEntity-&gt;getMimeType();

      // Get image file URI.
      $mediaImageEntityUri = $mediaImageEntity-&gt;getFileUri();

      // Set image URL.
      $url = file_create_url($mediaImageEntityUri);

      // Set image style URL.
      if ($imageStyle &amp;&amp; $mediaImageMimeType !== &quot;image/svg+xml&quot;) {
        $url = $this-&gt;entityTypeManager
          -&gt;getStorage('image_style')
          -&gt;load($imageStyle)
          -&gt;buildUrl($mediaImageEntityUri);
      }

      return [
        'url' =&gt; $url,
        'alt' =&gt; $alt,
        'width' =&gt; $width,
        'height' =&gt; $height,
        'mimeType' =&gt; $mediaImageMimeType,
        'title' =&gt; $title,
      ];
    }

    return NULL;
  }

}

Then in preprocess node, for example, you can call:
$variables['myVariable'] = \Drupal::service('chs')-&gt;getMediaFieldImage($node-&gt;get(&quot;field_name&quot;), &quot;image_style&quot;);
// Just need to pass the media field name. The image style is optional.

Then in a Twig template you can access as:
{{ myVariable.url }}
{{ myVariable.alt }}
{{ myVariable.width }}
{{ myVariable.height }}
{{ myVariable.mimeType }}
{{ myVariable.title }}

I've stripped down this code to show just the media entity image get, so let me know if there are issues and I can update if needed.

A shorter version (using preprocess_node()):
use Drupal\image\Entity\ImageStyle;
function MYTHEME_preprocess_node(&amp;$variables) {
  $node = $variables['node'];
  if ($node-&gt;hasField('field_name') &amp;&amp; $node-&gt;get('field_name')-&gt;entity) {
    $imageURI = $node-&gt;field_name-&gt;entity-&gt;getFileUri();
    $variables['myVariable'] = ImageStyle::load('image_style')-&gt;buildUrl($imageURI);
  }
}