I am attempting to programmatically create a media file. This is what I bought thus far:
$file_data = file_get_contents('http://www.africau.edu/pictures/default/pattern.pdf'); $file = file_save_data($file_data, 'public://3010.pdf', FILE_EXISTS_REPLACE); $media = Media::create([ 'bundle' => 'file', 'uid' => '0', 'field_media_file' => [ 'target_id' => $file->id(), ], ]); $media->setName('Hey') ->setPublished(TRUE) ->save();
Sadly this returns me Drupal Developer following error and Drupal Developer media file would not get saved:
Error: Name to a member perform getSource() on null in DrupalmediaEntityMedia->getSource() (line 138 of core/modules/media/src/Entity/Media.php).
What am I doing mistaken? How can I repair this error?