Hi. I'm creating a site that had lots of Youtube videos that where ported to drupal as standard nodes. I created a custom type called Video and used the Youtube field module to store the references to the corresponding video. All this was done programatically so the process didn't grab the thumbnail from the video (the poster image) when nodes where created.
This is not a problem since the thumbnail is created when the video is shown in a view I created to list them. However, there are old videos that have been deleted from Youtube, so when the module tries to get the info about them in order to create the thumbnail I'm getting this error messages for each video not found, plus a broken image file stored in the sites/default/files folder:
Notice: Undefined property: stdClass::$data en youtube_get_remote_image() (line 62 of /www/sites/all/modules/youtube/youtube.inc).
Notice: Trying to get property of non-object en youtube_get_remote_image() (line 62 of /www/sites/all/modules/youtube/youtube.inc).
Notice: Trying to get property of non-object en youtube_get_remote_image() (line 62 of /www/sites/all/modules/youtube/youtube.inc).
Notice: Undefined property: stdClass::$data en youtube_get_remote_image() (line 77 of /www/sites/all/modules/youtube/youtube.inc).
The pertinent code is in the youtube_get_remote_image() function:
$result = drupal_http_request($url);
$data = json_decode($result->data);
// Get the high quality default thumbnail.
$src = $data->data->thumbnail->hqDefault;
The error is generated because there's no validation when $data is retrieved and, if no video was found, the variable is empty. It's trivial to fix it by adding a check as follow:
$result = drupal_http_request($url);
$data = json_decode($result->data);
if (!isset($data->data)) {
//return FALSE;
}
// Get the high quality default thumbnail.
$src = $data->data->thumbnail->hqDefault;
Plus, this makes the module to create a nice default poster image.
Best regards.
| Comment | File | Size | Author |
|---|---|---|---|
| #8 | youtube-removed-video-thumbnail-error-2160105-8.patch | 1.17 KB | Anonymous (not verified) |
| #1 | youtube-removed-video-thumbnail-error-2160105-1.patch | 589 bytes | guschilds |
Comments
Comment #1
guschilds commentedThanks for catching this and suggesting a fix. I've given this a shot and have found a slightly different approach that I believe works better.
By returning FALSE as you suggest, the default (grey) image will have to be grabbed from YouTube every time the image should be used. It also cannot be passed through an image style (because it's remote), so it will always be improperly sized any time an image style is in use for these thumbnails.
An alternate approach is implemented in the attached patch. It grabs (using the same technique that returning FALSE would) and saves the default image when it isn't found on the first attempt. This saved image can then be passed through any image style and will be quicker to load each time, as it will come from the Drupal file system rather than youtube.com.
The patch applies to the 7.x-1.0 release. Let me know what you think. Thanks again!
Comment #2
guschilds commentedComment #3
proteo commentedHi gus! Thanks for the patch. Of course, you're right about the issues with my solution. Applied the patch and it worked great, I can see that now the thumbnail is stored in the local file system.
Problem solved! Best regards.
Comment #4
guschilds commentedThe patch from #1 has been committed to the 7.x-1.x branch.
This bug still needs to be fixed in the 8.x-1.x branch (I've tested and confirmed it exists). Going to mark this as "Needs work" and will be mentioning that on the project page.
Thanks for everything, Proteo!
Comment #5
guschilds commentedComment #6
guschilds commentedUpdating the title for consistency with 8.x issues.
Comment #7
guschilds commentedComment #8
Anonymous (not verified) commentedI ported the patch to 8.x and added a fix to the same function that moves method calls that don't throw exceptions out of the try block. The youtube_get_remote_image() function now gracefully handles errors such as the video not being found.
Comment #9
Anonymous (not verified) commentedComment #10
Anonymous (not verified) commentedUpdate: this patch has been subsumed into #1 from #2320311: Save high quality image by default
Comment #12
guschilds commentedI have confirmed the patch in #8 solves the problem against 8.x-1.0.0-beta2 and have committed it to the 8.x-1.x branch. The page should no longer fail when attempting to fetch the remote image of a video that doesn't exist. Thanks for this!
Comment #13
guschilds commented