I have Youtube URL such as: https://www.youtube.com/watch?v=asddasOds32

I need to create Media video youtube entity programmatically.

Can I have some example code to solve this task?

1 Answer 1


Code example:

use Drupal\media\Entity\Media;

$media = Media::create([
  'bundle'      => 'my_youtube',
  'uid'         => 1,
  'name'        => 'my_video',
   'field_media_oembed_video' => [
    'value' => 'https://www.youtube.com/watch?v=iZ53oOOTsdc',


Not the answer you're looking for? Browse other questions tagged or ask your own question.