0

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;
    }
10
  • Drupal\media\OEmbed\UrlResolver::getResourceUrl is being passed an illegal $url parameter. I think it expects a string. Have you attached a debugger?
    – cilefen
    Jun 27, 2022 at 20:19
  • the value of $link is a string. Adding a logger of \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
    – Ronnie
    Jun 27, 2022 at 20:46
  • Those are language warnings, not Drupal.
    – cilefen
    Jun 27, 2022 at 21:28
  • I understand that. I am passing a string as far as I can tell.
    – Ronnie
    Jun 27, 2022 at 22:39
  • 2
    @cilefen was correct. Not sure where I had my var_dump before that was giving me a string, but when I did 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.
    – Ronnie
    Jun 29, 2022 at 16:48

1 Answer 1

0

$link is likely an object. Cast it to a string because it is used to dereference an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.