Drupal\media\Entity\Media::prepareSave should convert URL object metadata to string before saving

Created on 7 June 2019, about 5 years ago
Updated 3 February 2023, over 1 year ago

Problem/Motivation

Media bundles allow for source values to be mapped to fields on the bundle. The mapping is executed in Drupal\media\Entity\Media::prepareSave. There is an issue with how URLs are processed. Sometimes the metadata value is a string. Other times it is a \Drupal\Core\Url object. When the mapped value is saved, the assumption is that the value is a string. Take a look at Drupal\media\OEmbed\Resource::getAuthorUrl. This method calls Drupal\Core\URL::fromUri, which expects to receive a string. If it receives a URL object, then the result is a fatal error.

Steps to reproduce

  1. Create a 'Remote Video' media bundle
  2. Create an 'Author URL' field (Link field
  3. Map this field to 'The URL of the author/owner'
  4. Attempt to add a YouTube video as a Media entity
  5. Note the following error for a Link field: Recoverable fatal error: Object of class Drupal\\Core\\Url could not be converted to string in /app/web/core/lib/Drupal/Core/Url.php on line 281

If the mapped field is a text field, then then the result is a different fatal error:

Or the following error for a Text field: <code> Uncaught PHP Exception Drupal\\Core\\Entity\\EntityStorageException: "Expected argument of type "string", "Drupal\\Core\\Url" given" at /app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php line 847

Proposed resolution

Drupal\media\Entity\Media::prepareSave can check if a particular metadata is a URL object. If so, convert the object to a string before saving.

Remaining tasks


Community review.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

None.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
MediaΒ  β†’

Last updated about 17 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States Chris Burge

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    @phenaproxima I like the idea personally as it seems to be addressing the root cause. Wonder if you'll be able to reuse the test case though.

Production build 0.69.0 2024