/** * Validate video URL. */ public function validateInput(&$element, FormStateInterface &$form_state, $form) { $input = $element['#value']; $video_id = youtube_get_video_id($input); if ($video_id && strlen($video_id) <= 20) { $video_id_element = array('#parents' => $element['#parents']); array_pop($video_id_element['#parents']); $video_id_element['#parents'][] = 'video_id'; $form_state->setValueForElement($video_id_element, $video_id); } elseif (!empty($input)) { $form_state->setError($element, t('Please provide a valid YouTube URL.')); } }
function youtube_get_image_attachment($url) { $video_id = youtube_get_video_id($url); if (!$video_id) { return false; } $video_image_option = 'youtube-image-' . $video_id; // Return the image that has already been uploaded for this video ID if ($v = get_option($video_image_option)) { if (isset($v['attachment_id']) && get_post((int) $v['attachment_id'])) { return $v; } } $image_url = 'http://img.youtube.com/vi/' . $video_id . '/0.jpg'; $attachment = ld_handle_upload_from_url($image_url); if (!empty($attachment['attachment_id'])) { update_option($video_image_option, $attachment, false); return $attachment; } return false; }