Token for first media Image in the body I need to display an image url in a metatag item.  The problem is on one of my node types, the editors only upload an image as a media entity inside the body. 

What is the best way to handle this?
I feel like there is a better way but I just quickly created a custom token.

&lt;?php

/**
 * @file
 * Node custom functionality.
 */

use Drupal\Core\Render\BubbleableMetadata;
use Drupal\image\Entity\ImageStyle;

/**
 * Implements hook_token_info().
 */
function my_custom_module_token_info() {
  $type = [
    'name' =&gt; t('My Custom'),
    'description' =&gt; t('My Custom Tokens.'),
    'needs-data' =&gt; 'node',
  ];

  $node['body_first_media_image_url'] = [
    'name' =&gt; t("Body First Media Image Url"),
    'description' =&gt; t("Body's first media image url."),
  ];

  return [
    'types' =&gt; ['node' =&gt; $type],
    'tokens' =&gt; ['node' =&gt; $node],
  ];
}

/**
 * Implements hook_tokens().
 */
function my_custom_module_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];

  if ($type == 'node' &amp;&amp; !empty($data['node'])) {
    /** @var \Drupal\node\NodeInterface $node */
    $node = $data['node'];

    foreach ($tokens as $name =&gt; $original) {
      switch ($name) {
        // The first media item embedded in body.
        case 'body_first_media_image_url':
          $dom = new DOMDocument();
          @$dom-&gt;loadHTML($node-&gt;body-&gt;value);
          $embedEntities = $dom-&gt;getElementsByTagName('drupal-entity');
          if ($embedEntities-&gt;length &gt; 0) {
            $uuid = $embedEntities-&gt;item(0)-&gt;getAttribute('data-entity-uuid');
            $media = \Drupal::entityManager()-&gt;loadEntityByUuid('media', $uuid);
            $url = ImageStyle::load('extra_large')-&gt;buildUrl($media-&gt;image[0]-&gt;entity-&gt;uri-&gt;value);
            $replacements[$original] = $url;
          }
          break;
      }
    }

  }

  return $replacements;
}