Problem/Motivation
When an oembed entity is fetched and there is an error fetching the resource a message is shown to the user saying "Could not retrieve the oEmbed resource", however, this happens on every visit, and this is shown also to anonymous users, and for them, it doesn't make sense.
Steps to reproduce
- Create an entity to be embedded with a valid URL
- Remove, hide or block the referenced entity (if it is a video mark it as private or delete it)
- Visit the page where the entity is supposed to be rendered.
Proposed resolution
Log the error instead of showing it in a message.
| Comment | File | Size | Author |
|---|---|---|---|
| #36 | core-3202896-mr-9357-20250410.patch | 1.59 KB | joegl |
| #33 | core-3202896-mr-9357-20250227.patch | 1.55 KB | joegl |
| oembed-error-msg.png | 554.73 KB | betoaveiga |
Issue fork drupal-3202896
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
betoaveigaAdding patch to log instead of sending a message on oEmbed fetch error.
Comment #3
cilefen commentedComment #4
joao.ramos.costa commentedMinimal change to #2.Comment #5
joao.ramos.costa commentedComment #6
joao.ramos.costa commentedMinimal change to #2.
(Sorry, a little problem in the previous patch.)
Comment #9
marcoscanoI agree this issue makes sense and it's a bug, since the "Could not retrieve the oEmbed resource" being displayed to anonymous visitors when a remote video stops being available isn't ideal.
However, I do think that's a good indicator for editors. So my suggestion is that instead of always logging, we should check
$media->access('update')- If the user can edit the media item, show the error on screen, so they can take action to fix the problem
- If the user can't edit, it's likely a visitor, in which case we should just just log the error.
And yes, we should definitely
return NULLin any case (as suggested by #6), otherwise the site will break hard when it tries to call methods on the non-existing$resourcevariable.Also, we need tests! :)
Comment #11
melonangie commentedComment #13
gerzenstl commentedPatch from #11 works fine.
Tested with Drupal 9.3.13
Comment #14
adrianodias commented#11 is good for me too.
Tested with Drupal 9.4.7
Comment #16
martijn de witAdded
10.1.x: PHP 8.1 & MySQL 5.7to #11 to see how it works out.Comment #17
adubovskoy commentedRewrote a bit issue-3202896-6.patch, added link for editing media entity.
Comment #18
martijn de witSounds like a good addition!
Only thing that is left is a proper test to move this issue forward.
Comment #20
anybodyShould we perhaps close this issue and merge efforts in #2972846: Improve oEmbed exception logging to make fixing these issues and testing more easy? Both are touching the message details and are very close... What do you think?
Comment #21
recrit commentedTo me the approach in this issue is better than #2972846: Improve oEmbed exception logging.
#2972846: Improve oEmbed exception logging: The change in
core/modules/media/src/OEmbed/ResourceFetcher.phpwill now expose a lot of information to an anonymous user when the fetcher error message is displayed by\Drupal\media\Plugin\media\Source\OEmbed::getMetadata(). That is not desired on any website.Comment #22
msnassar commentedWhen indexing media with unavailable resource using search api, I see the following error in the log:
Warning: Undefined variable $resource_url in Drupal\media\Plugin\media\Source\OEmbed->getMetadata() (line 253 of core/modules/media/src/Plugin/media/Source/OEmbed.php)Steps to reproduce:
- Create media entity using live stream video using e.g. youtube.
- From youtube, finish the steaming and make it unavailable.
- Use search api to index media.
- run search index.
- Check the log.
I think we should use media source field value to display the resource url as follows:
Comment #28
recrit commented@msnassar The
Undefined variable $resource_urlshould be fixed in the latest MR.I created an MR against 11.x with error message updated to the following. I removed the "|" from the message since system logs often are delimited with "|" which could cause issue with parsing the log.
Attached is a static patch of the MR for composer builds. Please contrib to the MR for any further development.
Comment #29
antonín slejška commentedThe patch issue-3202896-7.patch and the diff drupal-3202896-MR9357-11.x--20240828-1.diff look good for me.
Comment #30
smustgrave commentedHave not reviewed but was previously tagged for tests which still appear to be needed.
Comment #31
anybodySee #20 I still think it would make more sense to get that one finished instead...? (And yes, it's annoying for users ;))
Comment #33
joegl commentedApplied a patch from the Merge Request diff successfully to Drupal Core 10.3.13.
I reviewed the Merge Request in #2972846 first, but that issue does not add an access check before logging the error and does not appear to resolve the issue of this error showing to anonymous users. As another user commented in that issue, it appears the changes there are exposing even more detailed information to anonymous users which exacerbates the issue presented here.
Comment #34
loze commentedMaybe it could dispatch an event when the source isn't found?
I have a site with over 100,000 entities with media references, many of these have become old and the youtube urls are no longer active. I would like to be able to flag the posts so I can easily find them and remove them or take some appropriate action.
If i was able to listen for an event when there is an error I could do this.
Comment #35
joegl commentedI ran into a unique fatal error caused by the patch. In short, the media entity being used to generate the edit link for the error message will not always have an ID or valid edit link. I recommend adding a condition to check for the ID of the media entity before continuing.
In length, a content type with an oEmbed media entity will reference the media entity using an entity reference field. When using layout builder for the default display for the content type, the content preview for layout builder uses the generateSampleValue method on the entity reference item to generate a preview. The method attempts to find a random, valid entity to use as a sample value. If it cannot find one, it generates an entity on the fly. The entity generated on the fly is not saved, and does not have a valid ID. This throws a fatal error then when using this code because it attempts to generate an edit link on a media entity without an ID. I recommend adding a condition to check for the ID of the media entity before continuing.
Perhaps there's a better way to go about this then adding a check for the $media->id().
Comment #36
joegl commentedI added the conditional to the merge request and cut a new patch on 10.4.x
Comment #38
pameeela commentedThis sounds like a good follow up task as it expands the scope of this, and I think it would be better to fix the user-facing issue as originally reported first. This is definitely something I have noticed and agree it is a problem!
Comment #39
poker10 commentedRe #20 and #31 - I think this issue should be fixed regardless of the #2972846: Improve oEmbed exception logging, because the error should not be visible to anonymous users. The other issue's MR does not fix the message displayed for anonymous users, so let's keep both issues open.