-
-
Save steffenr/a40bab1f3b1c066d5c0655351e2107fd to your computer and use it in GitHub Desktop.
<?php | |
$filesystem = \Drupal::service('file_system'); | |
// Create file entity. | |
$image = File::create(); | |
$image->setFileUri($destination); | |
$image->setOwnerId(\Drupal::currentUser()->id()); | |
$image->setMimeType('image/' . pathinfo($destination, PATHINFO_EXTENSION)); | |
$image->setFileName($filesystem->basename($destination)); | |
$image->setPermanent(); | |
$image->save(); | |
// Create media entity with saved file. | |
$image_media = Media::create([ | |
'bundle' => 'image', | |
'uid' => \Drupal::currentUser()->id(), | |
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(), | |
'status' => Media::PUBLISHED, | |
'field_image' => [ | |
'target_id' => $image->id(), | |
'alt' => t('Placeholder image'), | |
'title' => t('Placeholder image'), | |
], | |
]); | |
$image_media->save(); |
Never use magic methods. They are unclear also not supported by IDE's. Use the content entity functions instead:
$media->set('field_description', $something);
$media->save;
The $something's structure depends on the field type. In case of simple text fields, it's just simple string. But if it's already a body field, probably it's going to be some nested array. Look examples in google, or try to find the field type itself in core.
Perhaps a better way to get you file mimetype:
\Drupal::service('file.mime_type.guesser')->guess($destination);
(from preCreate method of File entity)
Don't you need to set the thumbnail
and field_media_image
key in the Media entity too ?
For my case, without that, my media entity was associated with a default file.
"thumbnail" => [
"target_id" => $file->id(),
"alt" => $file->getFilename(),
],
"field_media_image" => [
"target_id" => $file->id(),
"alt" => $file->getFilename(),
],
Hi,
This is what I have
$name = basename($xml_record->image);
$destination = "public://$name";
$file = file_save_data(file_get_contents($xml_record->image), $destination, FILE_EXISTS_REPLACE);
$image_media = Media::create([
'bundle' => 'image',
'uid' => \Drupal::currentUser()->id(),
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
'field_image' => [
'target_id' => $file->id(),
'alt' => $name,
'title' => $name
],
]);
$image_media->setName($name)->setPublished(TRUE)->save();
the result is below. The image does not get attached. It is uploaded though. I am running Drupal 8.6.10 and php 7.4.1
Thanks @Adm94. I tried what you said and it now works fine. Thanks a lot.
You can directly pass the file Entity when you create the media like this :
$image_media = Media::create([
'bundle' => 'image',
'uid' => \Drupal::currentUser()->id(),
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
'field_image' => $file
]);
Hi, is it possible to load existing media image into a media field ? I used Media::create but the problem is, its creating duplicate in the media browser, I tied Media::load with the media id of the image, no luck.
any assistance would be really helpful.
To avoid duplicate in the media browser, you can use the form display entity reference autocomplete.
Hi Chris, thank you for the reply. If I use autocomplete it will difficult for the editor to browse the existing image they required.
Don't you need to set the
thumbnail
andfield_media_image
key in the Media entity too ?
For my case, without that, my media entity was associated with a default file."thumbnail" => [ "target_id" => $file->id(), "alt" => $file->getFilename(), ], "field_media_image" => [ "target_id" => $file->id(), "alt" => $file->getFilename(), ],
Thanks a lot, that worked with "thumbnail" 👍
Hi,
I am using 'media_entity_twitter' module to store the twitter URL and username using admin UI. I want to insert this tweet entity using drupal 8 custom module.
Any help?
Thanks