View source
<?php
function video_embed_field_field_info() {
return array(
'video_embed_field' => array(
'label' => 'Video Embed',
'description' => 'Embed videos from youtube or vimeo',
'settings' => array(),
'instance_settings' => array(
'allowed_types' => array(
'youtube',
'vimeo',
),
'description_field' => 0,
),
'default_widget' => 'video_embed_field_widget',
'default_formatter' => 'video_embed_field_formatter',
),
);
}
function video_embed_field_field_instance_settings_form($field, $instance) {
$settings = $instance['settings'];
$form['description_field'] = array(
'#type' => 'checkbox',
'#title' => t('Enable <em>Description</em> field'),
'#default_value' => isset($settings['description_field']) ? $settings['description_field'] : '',
'#description' => t('The description field allows users to enter a description about the video.'),
'#parents' => array(
'instance',
'settings',
'description_field',
),
'#weight' => 11,
);
$parser_form = isset($instance['settings']['playback_settings']) ? video_embed_field_get_form($instance['settings']['playback_settings']) : video_embed_field_get_form(array());
$form['playback_settings'] = array(
'#type' => 'vertical_tabs',
'#title' => t('Playback settings'),
'#weight' => 15,
) + $parser_form;
return $form;
}
function video_embed_field_field_widget_info() {
return array(
'video_embed_field_widget' => array(
'label' => 'Video Embed',
'description' => 'Provides a video embed field',
'field types' => array(
'video_embed_field',
),
'settings' => array(),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_DEFAULT,
),
),
);
}
function video_embed_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
$element += array(
'#type' => 'video_embed_field_widget',
);
$element['video_url'] = array(
'#type' => 'textfield',
'#title' => $element['#title'],
'#description' => $element['#description'],
'#attributes' => array(
'class' => array(
'video_embed_url',
),
),
'#attached' => array(
'css' => array(
drupal_get_path('module', 'video_embed_field') . '/video_embed_field.form.css',
),
),
'#default_value' => isset($items[$delta]['video_url']) ? $items[$delta]['video_url'] : '',
'#required' => $element['#required'],
);
if (!empty($instance['settings']['description_field'])) {
$element['description'] = array(
'#type' => 'textfield',
'#title' => t('Description'),
'#default_value' => isset($items[$delta]['description']) ? $items[$delta]['description'] : '',
'#description' => t('The description which may be used as a label.'),
);
}
return $element;
}
function video_embed_field_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $item) {
if (!empty($item['video_url'])) {
$item['video_url'] = trim($item['video_url']);
if (stristr($item['video_url'], '.') && !stristr($item['video_url'], 'http://') && !stristr($item['video_url'], 'https://')) {
$item['video_url'] = 'http://' . $item['video_url'];
}
$parts = parse_url($item['video_url']);
if (!$parts || !isset($parts['host'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => t('Invalid Url'),
'message' => t('Video: Invalid Video URL.', array(
'%name' => $instance['label'],
)),
);
}
else {
$host = $parts['host'];
if (stripos($host, 'www.') > -1) {
$host = substr($host, 4);
}
$handlers = video_embed_get_handlers();
if (!isset($handlers[$host]['function']) || !function_exists($handlers[$host]['function'])) {
$errors[$field['field_name']][$langcode][$delta][] = array(
'error' => t('Unsupported Video Provider'),
'message' => t('%name: This video provider is not currently supported.', array(
'%name' => $instance['label'],
)),
);
}
}
}
}
}
function video_embed_field_field_is_empty($item, $field) {
return empty($item) || empty($item['video_url']) || $item['video_url'] === '';
}
function video_embed_field_field_formatter_info() {
return array(
'video_embed_field_formatter' => array(
'label' => t('Video Embed Formatter'),
'field types' => array(
'video_embed_field',
),
),
'video_embed_field_formatter_teaser' => array(
'label' => t('Video Embed Formatter - Teaser'),
'field types' => array(
'video_embed_field',
),
),
'video_embed_field_formatter_image_large' => array(
'label' => t('Video Embed Formatter - Image - Large'),
'field types' => array(
'video_embed_field',
),
),
'video_embed_field_formatter_image_medium' => array(
'label' => t('Video Embed Formatter - Image - Medium'),
'field types' => array(
'video_embed_field',
),
),
'video_embed_field_formatter_image_small' => array(
'label' => t('Video Embed Formatter - Image - Small'),
'field types' => array(
'video_embed_field',
),
),
);
}
function video_embed_field_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
$handlers = video_embed_get_handlers();
foreach ($items as $delta => $item_wrapper) {
foreach ($item_wrapper as $key => $item) {
if (!stristr($item['video_url'], 'http://') && !stristr($item['video_url'], 'https://')) {
$item['video_url'] = 'http://' . $item['video_url'];
}
$parts = parse_url($item['video_url']);
if (!isset($parts['host'])) {
$items[$delta][$key]['embed_code'] = l($item['video_url'], $item['video_url']);
return;
}
$host = $parts['host'];
if (stripos($host, 'www.') > -1) {
$host = substr($host, 4);
}
$host = strtolower($host);
if (isset($handlers[$host]['function']) && function_exists($handlers[$host]['function'])) {
$display = $displays[$delta]['type'];
if (isset($instances[$delta]['settings']['playback_settings'])) {
$settings = $instances[$delta]['settings']['playback_settings'][$handlers[$host]['title']];
$settings = array_merge($handlers[$host]['defaults'], $settings);
}
else {
$settings = $handlers[$host]['defaults'];
}
if ($display == 'video_embed_field_formatter_teaser') {
$items[$delta][$key]['embed_code'] = call_user_func($handlers[$host]['function'], $item['video_url'], $settings, 'teaser');
}
else {
if ($display == 'video_embed_field_formatter_image_small') {
$items[$delta][$key]['embed_code'] = call_user_func($handlers[$host]['function'], $item['video_url'], $settings, 'image', 'small');
}
else {
if ($display == 'video_embed_field_formatter_image_medium') {
$items[$delta][$key]['embed_code'] = call_user_func($handlers[$host]['function'], $item['video_url'], $settings, 'image', 'medium');
}
else {
if ($display == 'video_embed_field_formatter_image_large') {
$items[$delta][$key]['embed_code'] = call_user_func($handlers[$host]['function'], $item['video_url'], $settings, 'image', 'large');
}
else {
$items[$delta][$key]['embed_code'] = call_user_func($handlers[$host]['function'], $item['video_url'], $settings);
}
}
}
}
}
else {
$items[$delta][$key]['embed_code'] = l($item['video_url'], $item['video_url']);
}
}
}
}
function video_embed_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
foreach ($items as $delta => $item) {
if (isset($item['description']) && $item['description']) {
$description = array(
'#prefix' => '<div class="video-embed-description">',
'#markup' => $item['description'],
'#suffix' => '</div>',
);
}
else {
$description = array();
}
$element[$delta] = array(
array(
'#markup' => $item['embed_code'],
),
$description,
);
}
return $element;
}
function video_embed_get_handlers() {
$handlers = cache_get('video_embed_field_handlers');
if ($handlers === FALSE) {
$available = module_invoke_all('video_embed_handler_info');
foreach ($available as $handler) {
$handlers[$handler['domain']] = $handler;
}
drupal_alter('video_embed_field_handlers', $handlers);
cache_set('video_embed_field_handlers', $handlers);
}
else {
$handlers = $handlers->data;
}
return $handlers;
}
function video_embed_field_filter_info() {
$filters['video_embed_field'] = array(
'title' => t('Video Embedding'),
'description' => t('Replaces [VIDEO::http://www.youtube.com/watch?v=someVideoID] tags with embedded videos.'),
'process callback' => 'video_embed_field_filter_process',
);
return $filters;
}
function video_embed_field_filter_process($text, $filter, $format) {
preg_match_all('/ \\[VIDEO:: ( [^\\[\\]]+ )* \\] /x', $text, $matches);
$tag_match = (array) array_unique($matches[1]);
$handlers = video_embed_get_handlers();
foreach ($tag_match as $video_url) {
if (!stristr($video_url, 'http://') && !stristr($video_url, 'https://')) {
$video_url = 'http://' . $video_url;
}
$parts = parse_url($video_url);
if (!isset($parts['host'])) {
$embed_code = l($video_url, $video_url);
}
else {
$host = $parts['host'];
if (stripos($host, 'www.') > -1) {
$host = substr($host, 4);
}
if (isset($handlers[$host]['function']) && function_exists($handlers[$host]['function'])) {
$defaults = $handlers[$host]['defaults'];
$embed_code = call_user_func($handlers[$host]['function'], $video_url, $defaults);
}
else {
$embed_code = l($video_url, $video_url);
}
}
$text = str_replace('[VIDEO::' . $video_url . ']', $embed_code, $text);
}
return $text;
}
function video_embed_field_video_embed_handler_info() {
$ytdefaults = array(
'height' => 349,
'width' => 560,
'teaser_height' => 349,
'teaser_width' => 560,
'autoplay' => 0,
'hd' => 1,
'rel' => 0,
'autohide' => 2,
'showinfo' => 1,
'theme' => 'dark',
);
return array(
'youtube' => array(
'domain' => 'youtube.com',
'function' => 'video_embed_field_handle_youtube',
'defaults' => $ytdefaults,
'form' => 'video_embed_field_handler_youtube_form',
'title' => 'Youtube',
),
'youtu.be' => array(
'domain' => 'youtu.be',
'defaults' => $ytdefaults,
'function' => 'video_embed_field_handle_youtube',
'title' => 'Youtube',
),
'vimeo' => array(
'domain' => 'vimeo.com',
'function' => 'video_embed_field_handle_vimeo',
'defaults' => array(
'height' => 315,
'width' => 560,
'teaser_height' => 315,
'teaser_width' => 560,
'color' => '00adef',
'autoplay' => 0,
),
'form' => 'video_embed_field_handler_vimeo_form',
'title' => 'Vimeo',
),
);
}
function video_embed_field_get_form($defaults) {
$handlers = video_embed_get_handlers();
$form = array();
foreach ($handlers as $domain => $handler) {
if (isset($handler['form']) && function_exists($handler['form'])) {
$handler_defaults = isset($defaults[$handler['title']]) ? $defaults[$handler['title']] : array();
$handler_defaults = array_merge($handler['defaults'], $handler_defaults);
$form[$handler['title']] = call_user_func($handler['form'], $handler_defaults);
$form[$handler['title']] += array(
'#type' => 'fieldset',
'#title' => t($handler['title']),
);
}
}
return $form;
}
function video_embed_field_handle_youtube($url, $settings, $type = 'video', $image_size = 'small') {
if (stristr($url, 'http://')) {
$url = substr($url, 7);
}
else {
if (stristr($url, 'https://')) {
$url = substr($url, 8);
}
}
$pos = strripos($url, 'v=');
if ($pos !== FALSE) {
$pos += 2;
$pos2 = stripos($url, '&', $pos);
}
else {
$pos = strripos($url, '/');
if ($pos !== FALSE) {
$pos++;
$pos2 = stripos($url, '?', $pos);
}
}
if ($pos === FALSE) {
return l($url, $url);
}
else {
if ($pos2 > 0) {
$id = substr($url, $pos, $pos2 - $pos);
}
else {
$id = substr($url, $pos);
}
}
if ($type == 'image') {
if ($image_size == 'large') {
return '<img src="' . 'http://img.youtube.com/vi/' . $id . '/0.jpg' . '" />';
}
else {
return '<img src="' . 'http://img.youtube.com/vi/' . $id . '/2.jpg' . '" />';
}
}
else {
$teaser = $type == 'teaser';
$height = $teaser ? $settings['teaser_height'] : $settings['height'];
$width = $teaser ? $settings['teaser_width'] : $settings['width'];
$hd = $settings['hd'];
$rel = $settings['rel'];
$autoplay = $settings['autoplay'];
$autohide = $settings['autohide'];
$showinfo = $settings['showinfo'];
$theme = $settings['theme'];
$template_start = '<iframe width="' . $width . '" height="' . $height . '" src="';
$template_end = '?hd=' . $hd . '&rel=' . $rel . '&wmode=opaque&autoplay=' . $autoplay . '&theme=' . $theme . '&autohide=' . $autohide . '&showinfo=' . $showinfo . '" frameborder="0" allowfullscreen></iframe>';
$url_temp = 'http://www.youtube.com/embed/';
return $template_start . $url_temp . $id . $template_end;
}
}
function video_embed_field_handler_youtube_form($defaults) {
$form = array();
$form['height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Height'),
'#description' => t('The height of the youtube player, in pixels. Only enter the number e.g. 349'),
'#default_value' => $defaults['height'],
);
$form['width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Width'),
'#description' => t('The width of the youtube player, in pixels. Only enter the number e.g. 560'),
'#default_value' => $defaults['width'],
);
$form['teaser_height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Height - Teaser'),
'#description' => t('The height of the youtube player, in pixels. Only enter the number e.g. 349'),
'#default_value' => $defaults['teaser_height'],
);
$form['teaser_width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Width - Teaser'),
'#description' => t('The width of the youtube player, in pixels. Only enter the number e.g. 560'),
'#default_value' => $defaults['teaser_width'],
);
$form['theme'] = array(
'#type' => 'select',
'#options' => array(
'dark' => t('Dark'),
'light' => t('Light'),
),
'#title' => t('Player theme'),
'#default_value' => $defaults['theme'],
);
$form['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#description' => t('Play the video immediately.'),
'#default_value' => $defaults['autoplay'],
);
$form['hd'] = array(
'#type' => 'checkbox',
'#title' => t('Use HD'),
'#description' => t('Attempt to play the video in HD if available.'),
'#default_value' => $defaults['hd'],
);
$form['rel'] = array(
'#type' => 'checkbox',
'#title' => t('Show related videos'),
'#description' => t('Show related videos after the video is finished playing.'),
'#default_value' => $defaults['rel'],
);
$form['showinfo'] = array(
'#type' => 'checkbox',
'#title' => t('Show info'),
'#description' => t('Display information like the video title and rating before the video starts playing.'),
'#default_value' => $defaults['showinfo'],
);
$form['autohide'] = array(
'#type' => 'radios',
'#options' => array(
0 => t('The video progress bar and player controls will be visible throughout the video.'),
1 => t('Automatically slide the video progress bar and the player controls out of view a couple of seconds after the video starts playing. They will only reappear if the user moves her mouse over the video player or presses a keyboard key.'),
2 => t('The video progress bar will fade out but the player controls (play button, volume control, etc.) remain visible.'),
),
'#title' => t('Autohide progress bar and the player controls'),
'#description' => t('Controls the autohide behavior of the youtube player controls.'),
'#default_value' => $defaults['autohide'],
);
return $form;
}
function video_embed_field_handle_vimeo($url, $settings, $type = 'video', $image_size = 'small') {
$pos = strripos($url, '/');
if ($pos === FALSE) {
return l($url, $url);
}
else {
$pos += 1;
$id = substr($url, $pos);
}
if ($type == 'image') {
$return = file_get_contents('http://vimeo.com/api/v2/video/' . $id . '.php');
$return = unserialize($return);
$video = current($return);
if ($image_size == 'large') {
$image_url = $video['thumbnail_large'];
}
else {
if ($image_size == 'medium') {
$image_url = $video['thumbnail_medium'];
}
else {
$image_url = $video['thumbnail_small'];
}
}
return '<img src="' . $image_url . '" />';
}
else {
$teaser = $type == 'teaser';
$height = $teaser ? $settings['teaser_height'] : $settings['height'];
$width = $teaser ? $settings['teaser_width'] : $settings['width'];
$color = $settings['color'];
$autoplay = $settings['autoplay'];
$template_start = '<iframe src="http://player.vimeo.com/video/';
$template_end = '?portrait=0&color=' . $color . '&autoplay=' . $autoplay . '" frameborder="0" height="' . $height . '" width="' . $width . '"></iframe>';
return $template_start . $id . $template_end;
}
}
function video_embed_field_handler_vimeo_form($defaults) {
$base_defaults = array(
'height' => 315,
'width' => 560,
'teaser_height' => 315,
'teaser_width' => 560,
'color' => '00adef',
'autoplay' => 0,
);
$defaults = array_merge($base_defaults, $defaults);
$form = array();
$form['height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Height'),
'#description' => t('The height of the vimeo player, in pixels. Only enter the number e.g. 315'),
'#default_value' => $defaults['height'],
);
$form['width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Width'),
'#description' => t('The width of the vimeo player, in pixels. Only enter the number e.g. 560'),
'#default_value' => $defaults['width'],
);
$form['teaser_height'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Height - Teaser'),
'#description' => t('The height of the vimeo player, in pixels. Only enter the number e.g. 315'),
'#default_value' => $defaults['teaser_height'],
);
$form['teaser_width'] = array(
'#type' => 'textfield',
'#size' => '5',
'#title' => t('Player Width - Teaser'),
'#description' => t('The width of the vimeo player, in pixels. Only enter the number e.g. 560'),
'#default_value' => $defaults['teaser_width'],
);
$form['color'] = array(
'#type' => 'select',
'#options' => array(
'00adef' => t('Blue'),
'ff9933' => t('Orange'),
'c9ff23' => t('Lime'),
'ff0179' => t('Fuschia'),
'ffffff' => t('White'),
),
'#title' => t('Player Color'),
'#description' => t('The color to use on the vimeo player.'),
'#default_value' => $defaults['color'],
);
$form['autoplay'] = array(
'#type' => 'checkbox',
'#title' => t('Autoplay'),
'#description' => t('Play the video immediately.'),
'#default_value' => $defaults['autoplay'],
);
return $form;
}
function video_embed_field_requirements($phase) {
if ($phase == 'runtime') {
$fields = array_filter(field_info_fields(), '_video_embed_field_array_filter');
$errors = array();
foreach ($fields as $field_name => $field) {
$instances = field_read_instances(array(
'field_name' => $field_name,
));
foreach ($instances as $key => $instance) {
if (!isset($instance['settings']['playback_settings'])) {
$errors[] = $instance['field_name'] . ' on ' . $instance['entity_type'] . ' type ' . $instance['bundle'];
}
}
}
if (!empty($errors)) {
$requirements = array(
'video_embed_field' => array(
'description' => t('Some video_embed_fields are using the old settings format. You will need to go set the preferences for each field manually. '),
'severity' => REQUIREMENT_ERROR,
'title' => t('Video Embed Field settings:'),
'value' => implode(', ', $errors),
),
);
return $requirements;
}
}
}
function _video_embed_field_array_filter($item) {
return isset($item['type']) && $item['type'] == 'video_embed_field' && !$item['deleted'];
}