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

Created on 7 June 2019, over 5 years ago
Updated 19 September 2024, 3 months 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 review

Version

11.0 πŸ”₯

Component
MediaΒ  β†’

Last updated about 22 hours ago

Created by

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

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

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024