Dear Experts,
I have stuck with a problem in drupal 8. Actually I need to create node programmatically but content type contains two media fields, one field for Image and another for YouTube link so problem is, I don't know how to submit data for these fields programmatically.
Can you help me urgently please?!
Thanks
| Comment | File | Size | Author |
|---|---|---|---|
| Untitled-1.jpg | 145.63 KB | mahpari |
Comments
Comment #2
marcoscanoSupport requests are not supposed to be prioritized, and try to use the already existing tags :)
If these fields are just entity_reference fields, you only need to pass the id of the target entities you want to refer to on each of them. It will be necessary then to have already created beforehand the media entities you want to refer to.
A good place to look for examples of this are the tests inside the module, for example this one:
http://cgit.drupalcode.org/media_entity/tree/tests/src/Kernel/TokensTest...
Comment #3
mahpari commentedThank you Dear Marcos for your respond but I want to add media programmatically when node is created.
here is my code if you have time can you please have a look, it is very urgent as I am reaching to deadline.
$node = Node::create([
// The node entity bundle.
'type' => 'article',
'langcode' => 'en',
'created' => $created_date,
'changed' => $created_date,
// The user ID.
'uid' => 1,
'moderation_state' => 'published',
'title' => $title,
'field_article_section' => array('target_id'=>$section_target_id),
'field_author' => 111,
'field_article_main_image' => array('image' => [
'target_id' => $file->id()
],
'field_media_in_library'=> 1
),
'field_article_main_video' => array(
'field_media_video_embed_field' => $youtubeID,
'field_media_in_library'=> 1
),
'field_article_body_summary' => [
'summary' => substr(strip_tags($text), 0, 100),
'value' => $text,
'format' => 'rich_text'
]
]);
Thanks,
Mahpari
Comment #4
mahpari commentedComment #5
marcoscanoYou need to create the media entities before creating the node.
Something like this should work (untested):
Comment #6
mahpari commentedThanks a lot, let me try it and I will come back to you with the result.
Comment #7
mahpari commentedThank you Marcos, your code works like a charm. you saved me!
Have a great time.
Regards,
Mahpari
Comment #9
killua99 commentedAnything has changes since the last example code?
I'm trying create medias exactly how are you showing here, the thing in my case are. I can see a thumbnail image on the media overview, the node. But when I edit the media the image field is empty. The file entity is showing the file is been using 2 times, and one of them is on the media. Weird case is the media seems to lose the reference. How could double check / fix this situation ?
Comment #10
mojiferousKillua99 I had the same issue, and it was because of the langcode, if I removed the langcode value from the array it saved the image correctly!
Comment #11
killua99 commentedThanks you mojiferous! I did what you said and it works! This code should be added in the Module Documentation.
Comment #12
1an commentedHi I am looking for a similar solution but I need to find the image fid before I start this process? I am trying to use it for a migration where there are multiple nodes that have the same image. Currently a media entity is being created for each one and I would like to assign one media entry to each node.
I have been unable to find a solution in my research as yet.
Comment #13
Greenskin85 commented1an just generate a lookup array while migrating. something like
You might just save the media ID when no need of more info
Comment #14
sarguna raj m commentedHi @marcoscano,
tired #5 but it's not working and getting error as "The website encountered an unexpected error. Please try again later.
Error: Undefined class constant 'PUBLISHED'".
Thanks.
Comment #15
alex g commented@sarguna raj M
You should replace:
'status' => Media::PUBLISHEDWith
'status' => 1,