Using Drupal 9.4.1, I am creating a remote video media entity programmatically and for some reason the title and thumbnail are not being created. They are created when cron runs so in my terminal when I run drush cron
I get these errors:
$ drush cron
[notice] Starting CPC XML Import
[warning] Illegal offset type UrlResolver.php:154
[warning] Illegal offset type in isset or empty UrlResolver.php:155
[warning] Illegal offset type UrlResolver.php:167
[warning] Illegal offset type UrlResolver.php:168
The file that is producing this error is: /core/modules/media/src/OEmbed/UrlResolver.php
The media entities are created and attached to the node, however, no thumbnail and no title on the media entity.
An example URL I am using to create these entities is: https://www.youtube.com/watch?v=1r4wuc_oGwY
If I create the remote video entity through the media library UI, it creates it no problem. Thumbnail is generated and title is imported from YT.
My code is below:
// Add Video Gallery Videos.
if ($product_data->xpath('./marketing_content/content[@type="gallery-video"]')) {
foreach ($product_data->xpath('./marketing_content/content[@type="gallery-video"]/content') as $video) {
$link = $video->media->source; // This is the youtube link.
$video = self::upsertVideo($link);
$product->field_videos->appendItem(['target_id' => $video->id()]); // This works, it adds the media entity to the node.
}
}
and then here is my upsertVideo function:
/**
* Load or create a media type of remote_video based off of the URL.
*
* @param string $link
* A link to the video on CPC feed.
*
* @return \Drupal\media\Entity\Media
* The existing or newly created media entity.
*/
private static function upsertVideo($link) {
$query = \Drupal::entityQuery('media')
->condition('bundle', 'remote_video')
->condition('field_media_oembed_video', $link)
->execute();
// Video found, return it.
if (count($query) > 0) {
$video = Media::load(reset($query));
return $video;
}
// Create a new Remote Video media entity.
$video = Media::create([
'bundle' => 'remote_video',
'uid' => \Drupal::currentUser()->id(),
'field_media_oembed_video' => [
'value' => $link,
],
]);
$video->setPublished(TRUE)->save();
return $video;
}
\Drupal::logger('test')->notice('url is ' . $url);
to the top of getResourceUrl() produces the same log with a[notice] url is https://www.youtube.com/watch?v=1r4wuc_oGwY
gettype($url)
it was my simpleXMLObject. The fix was to cast it as a string$link = (string) $video->media->source; // This is the youtube link.