View source
<?php
function media_vimeo_file_formatter_info() {
$formatters['media_vimeo_video'] = array(
'label' => t('Vimeo Video'),
'file types' => array(
'video',
),
'default settings' => array(),
'view callback' => 'media_vimeo_file_formatter_video_view',
'settings callback' => 'media_vimeo_file_formatter_video_settings',
);
foreach (array(
'width',
'height',
'preview_uri',
'api',
'autoplay',
'byline',
'color',
'loop',
'portrait',
'title',
'protocol',
) as $setting) {
$formatters['media_vimeo_video']['default settings'][$setting] = media_vimeo_variable_get($setting);
}
$formatters['media_vimeo_image'] = array(
'label' => t('Vimeo Preview Image'),
'file types' => array(
'video',
),
'default settings' => array(
'image_style' => '',
),
'view callback' => 'media_vimeo_file_formatter_image_view',
'settings callback' => 'media_vimeo_file_formatter_image_settings',
);
return $formatters;
}
function media_vimeo_file_formatter_video_view($file, $display, $langcode) {
$scheme = file_uri_scheme($file->uri);
if ($scheme == 'vimeo' && empty($file->override['wysiwyg'])) {
$element = array(
'#theme' => 'media_vimeo_video',
'#uri' => $file->uri,
'#options' => array(),
);
foreach (array(
'width',
'height',
'api',
'autoplay',
'byline',
'color',
'loop',
'portrait',
'title',
'protocol',
) as $setting) {
$element['#options'][$setting] = isset($file->override[$setting]) ? $file->override[$setting] : $display['settings'][$setting];
}
return $element;
}
}
function media_vimeo_file_formatter_video_settings($form, &$form_state, $settings) {
$element = array();
$element['width'] = array(
'#title' => t('Width'),
'#type' => 'textfield',
'#default_value' => $settings['width'],
'#element_validate' => array(
'_vimeo_validate_video_width_and_height',
),
);
$element['height'] = array(
'#title' => t('Height'),
'#type' => 'textfield',
'#default_value' => $settings['height'],
'#element_validate' => array(
'_vimeo_validate_video_width_and_height',
),
);
$element['color'] = array(
'#title' => t('Custom video controls color'),
'#type' => 'textfield',
'#default_value' => $settings['color'],
'#description' => t('A 3 or 6 character hex color code'),
'#maxlength' => 7,
'#size' => 7,
'#element_validate' => array(
'_vimeo_validate_video_color',
),
);
$element['protocol'] = array(
'#title' => t('Iframe protocol'),
'#type' => 'radios',
'#options' => array(
'http://' => t('http://'),
'https://' => t('https://'),
'//' => t('Relative protocol (may fail if content is viewed outside of a web browser, e.g., email and RSS)'),
),
'#default_value' => $settings['protocol'],
'#description' => t('There may be issues with the player if the iframe protocol and page protocol do not match.'),
);
$element['autoplay'] = array(
'#title' => t('Autoplay video on load'),
'#type' => 'checkbox',
'#default_value' => $settings['autoplay'],
);
$element['loop'] = array(
'#title' => t('Loop video'),
'#type' => 'checkbox',
'#default_value' => $settings['loop'],
);
$element['title'] = array(
'#title' => t('Show the video title'),
'#type' => 'checkbox',
'#default_value' => $settings['title'],
);
$element['byline'] = array(
'#title' => t('Show the video byline'),
'#type' => 'checkbox',
'#default_value' => $settings['byline'],
);
$element['portrait'] = array(
'#title' => t("Show the user's portrait"),
'#type' => 'checkbox',
'#default_value' => $settings['portrait'],
);
$element['api'] = array(
'#title' => t('Enable the <a href="http://developer.vimeo.com/player/js-api">Javascript API</a>'),
'#type' => 'checkbox',
'#default_value' => $settings['api'],
'#description' => t('A required id in the format <code>media-vimeo-{video_id}</code> will be added to each iframe.'),
);
return $element;
}
function _vimeo_validate_video_width_and_height($element, &$form_state, $form) {
if (!empty($element['#value']) && !preg_match('/^(auto|([0-9]*(\\.[0-9]+)?%?))$/', $element['#value'])) {
form_error($element, t("The value entered for @dimension is invalid. Please insert a unitless integer for pixels, a percent, or \"auto\". Note that percent and auto may not function correctly depending on the browser and doctype.", array(
'@dimension' => $element['#title'],
)));
}
}
function _vimeo_validate_video_color($element, &$form_state, $form) {
if (!empty($element['#value']) && !preg_match('/^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/', $element['#value'])) {
form_error($element, t("The color value is invalid. Please use a 3 or 6 character hex color code.", array(
'@dimension' => $element['#title'],
)));
}
}
function media_vimeo_file_formatter_image_view($file, $display, $langcode) {
$scheme = file_uri_scheme($file->uri);
if ($scheme == 'vimeo') {
$wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);
$image_style = $display['settings']['image_style'];
$valid_image_styles = image_style_options(FALSE);
if (empty($image_style) || !isset($valid_image_styles[$image_style])) {
$element = array(
'#theme' => 'image',
'#path' => $wrapper
->getOriginalThumbnailPath(),
'#alt' => $file->filename,
);
}
else {
$element = array(
'#theme' => 'image_style',
'#style_name' => $image_style,
'#path' => $wrapper
->getLocalThumbnailPath(),
'#alt' => $file->filename,
);
}
return $element;
}
}
function media_vimeo_file_formatter_image_settings($form, &$form_state, $settings) {
$element = array();
$element['image_style'] = array(
'#title' => t('Image style'),
'#type' => 'select',
'#options' => image_style_options(FALSE),
'#default_value' => $settings['image_style'],
'#empty_option' => t('None (original image)'),
);
return $element;
}
function media_vimeo_file_default_displays() {
$default_displays = array();
$default_video_settings = array(
'media_large' => array(
'width' => 560,
'height' => 315,
'autoplay' => FALSE,
),
'media_original' => array(
'width' => 640,
'height' => 480,
'autoplay' => media_vimeo_variable_get('autoplay'),
),
);
foreach ($default_video_settings as $view_mode => $settings) {
$display_name = 'video__' . $view_mode . '__media_vimeo_video';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'weight' => 1,
'settings' => $settings,
);
}
$default_image_styles = array(
'media_preview' => 'square_thumbnail',
'media_large' => 'large',
'media_original' => '',
);
foreach ($default_image_styles as $view_mode => $image_style) {
$display_name = 'video__' . $view_mode . '__media_vimeo_image';
$default_displays[$display_name] = (object) array(
'api_version' => 1,
'name' => $display_name,
'status' => 1,
'weight' => 2,
'settings' => array(
'image_style' => $image_style,
),
);
}
return $default_displays;
}