video_embed_instagram.module in Video Embed Instagram 7
Add a handler for instagram videos to Video Embed Field.
See also
video_embed_field.api.php for more documentation
File
video_embed_instagram.moduleView source
<?php
/**
* @file
* Add a handler for instagram videos to Video Embed Field.
*
* @see video_embed_field.api.php for more documentation
*/
define('VIDEO_EMBED_INSTAGRAM_DEFAULT_WIDTH', 640);
define('VIDEO_EMBED_INSTAGRAM_DEFAULT_HEIGHT', 640);
/**
* Implements hook_video_embed_handler_info().
*
* This function is used to tell video_embed_field which functions will be used
* to handle different operations, along with a bit of metadata.
*/
function video_embed_instagram_video_embed_handler_info() {
$handlers = array();
$handlers['instagram'] = array(
'title' => 'Instagram Video',
'function' => 'video_embed_instagram_handle_video',
// Thumbnail_function is optional and takes the url
// and returns the thumbnail url.
'thumbnail_function' => 'video_embed_instagram_handle_thumbnail',
'form' => 'video_embed_instagram_form',
'domains' => array(
'instagram.com',
),
// Defaults are the defaults to provide to your form
// (as defined in your form callback).
'defaults' => array(
'width' => VIDEO_EMBED_INSTAGRAM_DEFAULT_WIDTH,
'height' => VIDEO_EMBED_INSTAGRAM_DEFAULT_HEIGHT,
),
);
return $handlers;
}
/**
* Implements hook_theme().
*
* Using video_embed_instagram.tpl.php as template.
*/
function video_embed_instagram_theme() {
return array(
'video_embed_instagram' => array(
'template' => 'video_embed_instagram',
'arguments' => array(
'url' => NULL,
'width' => NULL,
'height' => NULL,
),
),
);
}
/**
* Configuration form (the callback for the form key in info).
*
* Provide a form to configure out video settings.
*/
function video_embed_instagram_form($defaults) {
$form = array();
// Element for width.
$form['width'] = array(
'#type' => 'textfield',
'#title' => t('Player Width'),
'#description' => t('The width of the player.'),
'#default_value' => $defaults['width'],
'#element_validate' => array(
'element_validate_integer_positive',
),
);
// Form element for the height of the player -
// note we're using the default from defaults.
$form['height'] = array(
'#type' => 'textfield',
'#title' => t('Player Height'),
'#description' => t('The height of the player.'),
'#default_value' => $defaults['height'],
'#element_validate' => array(
'element_validate_integer_positive',
),
);
return $form;
}
/**
* This is the video handler (the 'function' key from handler_info).
*
* Function handles url and returns embed video html code.
*
* @param string $url
* This is string witch user input into video field.
* @param array $settings
* Settings which were defined by admin.
*
* @return mixed
* Return rendered html code or empty string if fails to get id from url.
*/
function video_embed_instagram_handle_video($url, array $settings) {
$output = '';
$instagram_video_id = _video_embed_instagram_get_video_id($url);
if ($instagram_video_id) {
// Embed code.
$embed = theme('video_embed_instagram', array(
'url' => '//instagram.com/p/' . $instagram_video_id . '/embed/',
'width' => $settings['width'],
'height' => $settings['height'],
));
// Return a render array.
$video = array(
'#markup' => $embed,
);
$output = $video;
}
else {
drupal_set_message(t('Error with getting id of the video.'), 'error');
}
// Just return an empty string if there is no id,
// so we don't have broken embeds showing up.
return $output;
}
/**
* Retrieve the thumbnail for the instagram video.
*
* @param string $url
* This is string witch user input into video field.
*
* @return array|bool
* Return FALSE if fails to get id from instagram.
*/
function video_embed_instagram_handle_thumbnail($url) {
$id = _video_embed_instagram_get_video_id($url);
if ($id) {
$image_url = 'http://instagr.am/p/' . $id . '/media/?size=l';
$request = drupal_http_request($image_url);
if ($request->code == 200 && !empty($request->redirect_url)) {
return array(
// Generally the id that the provider uses for the video.
'id' => $id,
// The url of the thumbnail.
'url' => $request->redirect_url,
);
}
else {
drupal_set_message(t('Error with getting thumbnail of the video.'), 'error');
}
}
return FALSE;
}
/**
* Helper function to take an instagram video url and return its id.
*
* @param string $url
* This is string witch user input into video field.
*
* @return string|bool
* Return FALSE if fails to get id using regexp on user input string.
*/
function _video_embed_instagram_get_video_id($url) {
$output = FALSE;
$url = check_plain($url);
// Parse_url is an easy way to break a url into its components.
$matches = array();
$url = preg_replace('/\\?.*/', '', $url);
// Grab id of instagram_video to get thumbnail for it and construct proper
// iframe.
preg_match('/instagram\\.com\\/p\\/(.*)[\\/]?/', $url, $matches);
if ($matches && !empty($matches[1])) {
// If url was with '/' symbol, clear it.
$output = rtrim($matches[1], '/');
}
return $output;
}
/**
* Implements hook_preprocess_hook().
*/
function video_embed_instagram_preprocess_video_embed_instagram(&$variables) {
if (!empty($variables['width'])) {
$variables['width'] = check_plain($variables['width']);
}
if (!empty($variables['height'])) {
$variables['height'] = check_plain($variables['height']);
}
}
Functions
Name | Description |
---|---|
video_embed_instagram_form | Configuration form (the callback for the form key in info). |
video_embed_instagram_handle_thumbnail | Retrieve the thumbnail for the instagram video. |
video_embed_instagram_handle_video | This is the video handler (the 'function' key from handler_info). |
video_embed_instagram_preprocess_video_embed_instagram | Implements hook_preprocess_hook(). |
video_embed_instagram_theme | Implements hook_theme(). |
video_embed_instagram_video_embed_handler_info | Implements hook_video_embed_handler_info(). |
_video_embed_instagram_get_video_id | Helper function to take an instagram video url and return its id. |
Constants
Name | Description |
---|---|
VIDEO_EMBED_INSTAGRAM_DEFAULT_HEIGHT | |
VIDEO_EMBED_INSTAGRAM_DEFAULT_WIDTH | @file Add a handler for instagram videos to Video Embed Field. |