I am trying to programmatically create a media object and a file, and then associate the file with the media object. The first part works--I can successfully create the media object and file. But the second part fails--the media object has no file associated with it.
Here is my code:
//Begin handling file uploads $file = file_save_data('hey dude', 'public://2019-07/my_example.txt', FILE_EXISTS_REPLACE); $media_arr = [ 'bundle' => 'file', 'langcode' => 'en', 'uid' => '0', 'name'=> 'Test Name', 'created' => date('U'), 'changed' => date('U'), 'moderation_state' => 'published', 'file' => [ 'target_id' => $file->id(), ], ]; $drupalMedia = Media::create($media_arr); $drupalMedia->setName('Test Media Object') ->setPublished(TRUE) ->save();
my_example.txt is created in the files directory, and the media object "Test Media Object" is also created, and I'm able to attach it to a node. But there is no file in the new media object! Am I missing something here?