use Drupal\media\Entity\Media;
// Model: https://www.youtube.com/embed/234Dfq985c1?enablejsapi=1&rel=0&showinfo=0&autoplay=1
$second_part = explode("/", $iframe_code)[4];
$video_code = explode("?", $second_part)[0];
$wellformed_url = "https://youtube.com/watch?v=" . $video_code;
// Create the new media item.
$image_video = Media::create([
'bundle' => 'remote_video',
'uid' => \Drupal::currentUser()->id(),
'langcode' => \Drupal::languageManager()->getDefaultLanguage()->getId(),
'field_media_oembed_video' => $wellformed_url,
]);
$image_video->save();
// Add the media item to a node.
$new_node->field_associated_videos = $image_video;
//Save the new node.
$new_node->save();
Created
June 9, 2021 18:49
-
-
Save davidjguru/67ae80794cb61bc76d750ce22b8426c7 to your computer and use it in GitHub Desktop.
Drupal 8 || 9 - How to create a media (remote video) entity node programmatically
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment