Published Feb 21. 2018 - 3 months ago
Updated or edited Feb 28. 2018

Drupal, YouTube and Vimeo integration

The GFF video section uses a few neat cowboy tricks to smoothly integrate videos from YouTube and Vimeo into the site

Nerd alert

This is NOT about fly fishing or fly tying, but about site development and nerdy stuff

The video page
The video page />The video section on the Global FlyFisher was a success basically from the day it was launched in December 2009 and that hasn't changed very much since then.Martin Joergensen
Neither has the system itself.
It has used Drupal all the way – even from before the main site was Drupal – and has had some nice behind-the-scenes features almost from day one.

The main protagonist

The main protagonist in the video system is and has always been the Embedded Media Field module for Drupal. This is one of the simplest of the many video handling modules available for Drupal, and does basically all that I need and no more – just as I like it. There are many other media handlers, but I find most of them overly complex, convoluted and in many cases hard to work with.
Not so the Embedded Media Field module.
As the name implies that it embeds media... including video. Just what I need and want.
It handles Vimeo and YouTube out of the box, but can be expanded with support for other suppliers such as Facebook, Vine, Dailymotion and many more, as well as simple HTML5 embedding of your self hosted videos. Since GFF has basically always supported Vimeo and YouTube and nothing else, the module has fit like hand in a glove.
It's fairly simple in its principle, being a field that you can add to any content type. It takes a URL as its value, and can then present the video in many shapes and forms.
I use it to produce the embed code for the single video node, simply showing the player hosted by the provider. In other places I show thumbprints linking to the individual video pages on GFF.
Being a plain field, it's supported by essentially all relevant Drupal functions and modules such as Views, the search function and many more.

Video node type

On GFF it's contained in a video content type, which also has many other fields. A title of course, and originator, description, keywords, category and a few more. All this gives enough information to organize the videos and present them where it's relevant.
In the beginning I had to manually enter all data into the fields, but since the information already all exists on the provider's page, I developed a small module that can take the video URL and fill the relevant fields in the video form. This saves a lot of typing, since most fields are copied over and can just be edited – or left as they are – before saving a video.
The module uses he API's provided by YouTube (Google) and Vimeo, and relies on AJAX to fetch data and fill the forms without any page refreshes.
By adding an AJAX function through form_alter, I can simply take the video URL entered into the URL field and fetch the appropriate data from either YouTube or Vimeo.

The process

The process is as follows:

  • The system establishes the provider based on the URL. Vimeo is vimeo.com, YouTube is youtube.com or yotu.be for short links.
  • It then isolates the video ID, again from the URL.
  • It checks if the video has been submitted already by looking for provider/ID matches in the GFF database.
  • If the video is new, it uses Curl or a simple file_get_contents to get the data from the provider.
  • YouTube uses the form https://www.googleapis.com/youtube/v3/videos?id=[id]&part=snippet&key=[key] and Vimeo the form http://vimeo.com/api/v2/video/[id].xml where [id] is the video ID and [key] is an API key required for YouTube.
  • The call returns Json for YouTube or XML for Vimeo, and all I need to do is parse the data and add it to the proper fields in the form.
  • If the call fails – like on a faulty URL – an error message is emitted.
  • The form changes are handled using the Drupal ajax_command_replace-function and the #commands form key, which enters the data in the proper fields by field ID.
  • Setting $form_state['rebuild'] = TRUE forces Drupal to re-render the form showing the updated fields.
  • Once that is done, the form can be further edited and saved.

The advantage of this method is that it uses Drupal's own AJAX system and allows for proper form altering and decent error handling.

Administor's form
The user's form
The forms
Martin Joergensen

One form, two forms

The form exists in two shapes. Internally it's the same form, but only shows a limited number of fields to the common user, and also shows it in the frontend theme rather than the admin theme. This allows registered users to suggest videos and use the function in a simple form shown in a recognizable environment.
Once the video has been saved by the user, it's still unpublished, and when I open it to review it, I see all the fields and can edit or fill them and publish the video if it's suitable.

Publishing

The videos are shown as individual nodes using the various relevant view modes – full, teaser etc. – and most pages, lists, blocks and overviews are made using core or Views.
A few functions are custom made, like the related content box, which I want to prioritize articles over videos, and the originator list, which is a little difficult to make using Views because the originator field is a simple text field and not a relation to a list of originator entities. This is a thing I'd like to change. The text field opens up for all kinds of errors, and a relation is much better.
But apart from that the system delivers. It currently hosts more than 9,000 videos and this number is steadily growing. It has no issues at all with handling traffic and integrates well with all other parts of the site and the search, caching, newsletter and other systems.

Missing video
Missing video />Martin Joergensen

Future development

I have long been annoyed by videos disappearing. with 9,000 videos embedded, some will be removed, moved or hidden by their originator and not work on GFF anymore.
This leads to an unbecoming error on the video page, and I usually simply remove the video when I find one that doesn't exist anymore. But using the same function as the one used to fill the form, I should be able to check for the existence of each video.
Setting up a cron-function that checks a number of videos now and then, it should be possible to automatically go through all videos and unpublish or delete those that fail.
I'd also like to fix the aforementioned problem with the originators. Many are users on GFF, and tying their user profile together with their videos would be good. This will require some programming to convert and transfer data from the text field to a proper relation, but should be fairly straightforward to make and execute.

Update February 28th 2018: I did it

I managed to make a small routine that actually checks the videos for validity. It's fairly simple, and works for now, but will need a little polish to work more smoothly.
It's a simple function called via a menu item only accessible by me.
- The function selects 40 random videos - the number was suitable in relation to time of execution.
- It then "deciphers" the video to get provider (YouTube, Vimeo) and ID.
- It then uses the same function as described above to fetch video data from the source.
- If data are found, the video is considered valid.
- If not, there's a problem, and the video is unpublished and marked as "Not found" in a field made for the purpose.
- I can the look at all unpublished and "unfound" videos in a View and decide their destiny.
- Some can be salvaged. They might have moved (changed URL) or simply failed when read through the API. These are edited, normalized and published.
- The rest appear in the content overview and can be filtered as unpublished video nodes and bulk deleted.
The random selection has its limits of course, finding fewer and fewer failing videos the more times I run it and tidy the list. But for now it still finds a fair number and gives me some videos to cull.
The next step will be making a cron function to go through all videos in sequence, a limited number at the time, keeping track of the progress, and then me running through the culled ones now and then.

.

Log in or register to pre-fill name on comments, add videos, user pictures and more.
Read more about why you should register.
 

Since you got this far …

The GFF money box
The GFF money box
Martin Joergensen

… I have a small favor to ask.

Long story short

Support the Global FlyFisher through several different channels, including PayPal.

Long story longer

The Global FlyFisher has been online since the mid-90's and has been free to access for everybody since day one – and will stay free for as long as I run it.
But that doesn't mean that it's free to run.
It costs money to drive a large site like this.

See more details about what you can do to help in this blog post.