Adding an image to a media bundle's field I am trying to add create a piece of new media with the following code. When visiting the media overview screen the media item is listed along with a thumbnail however when you edit the media item the file is not attached, although the file exists in the files area?

use Drupal\Core\Language\Language;
use Drupal\media_entity\Entity\Media;

$data =     file_get_contents("https://homepages.cae.wisc.edu/~ece533/images/airplane.png");
$file = file_save_data($data, 'public://plane.jpg',  FILE_EXISTS_RENAME);


$image_media = Media::create([
  'bundle' =&gt; 'image',
  'uid' =&gt; '1',
  'langcode' =&gt; Language::LANGCODE_DEFAULT,
  'status' =&gt; Media::PUBLISHED,
  'field_image' =&gt; [
    'target_id' =&gt; $file-&gt;id() ,
    'alt' =&gt; t('this is the alt text'),
    'title' =&gt; t('this is an image'),
  ],
]);
$image_media-&gt;save();
Unless you have a default language setup, Language::LANGCODE_DEFAULT will resolve to x-default.

Either:


Set a default language
Set the property manually, e.g, 'en' rather than Language::LANGCODE_DEFAULT
Remove the line entirely