I using a managed file form field to upload an image which I then want to assign to a field on a node.

I see a media image entity is created like this

$values = [
      'targetEntityType' => "media",
      'bundle' => "image",
      'status' => true
    ];

    $imageEntity = Media::create($values);

Now i'm stuck. Some pointers in the right direction would be excellent.

Comments

AverilL created an issue. See original summary.

marcoscano’s picture

Status: Active » Fixed
Issue tags: +D8Media

You can see an example here: #2813025: Create Drupal8 Node Programmatically that contains two media fields

Feel free to re-open if it is not clear enough

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

slefevre1’s picture

In the example code cited, there are these lines:

 $image_media = Media::create([
      'bundle' => 'your_image_bundle_name_here',

How does one figure out what the bundle name is?

spfaffly’s picture

#2832079-4: How to create Media Entity Image programatically and assign to field on a node

You can lookup the bundle name in the "Media Upload" config page in the admin screen:
/admin/config/media/upload