t('Video Filter'),
'description' => t('Substitutes [video:URL] with embedded HTML.'),
'process callback' => '_video_filter_process',
'settings callback' => '_video_filter_settings',
'default settings' => array(
'video_filter_width' => '400',
'video_filter_height' => '400',
'video_filter_autoplay' => 1,
'video_filter_related' => 1,
'video_filter_html5' => 1,
'video_filter_codecs' => _video_filter_map_codecs_name(video_filter_get_codec_info()),
'video_filter_multiple_sources' => TRUE,
),
'tips callback' => '_video_filter_tips',
// See http://drupal.org/node/1061244.
'weight' => -1,
);
return $filters;
}
function _video_filter_settings($form, &$form_state, $filter, $format, $defaults, $filters) {
$settings['video_filter_width'] = array(
'#type' => 'textfield',
'#title' => t('Default width setting'),
'#default_value' => isset($filter->settings['video_filter_width']) ? $filter->settings['video_filter_width'] : $defaults['video_filter_width'],
'#maxlength' => 4,
);
$settings['video_filter_height'] = array(
'#type' => 'textfield',
'#title' => t('Default height setting'),
'#default_value' => isset($filter->settings['video_filter_height']) ? $filter->settings['video_filter_height'] : $defaults['video_filter_height'],
'#maxlength' => 4,
);
$settings['video_filter_autoplay'] = array(
'#type' => 'radios',
'#title' => t('Default autoplay setting'),
'#description' => t('Not all video formats support this setting.'),
'#default_value' => isset($filter->settings['video_filter_autoplay']) ? $filter->settings['video_filter_autoplay'] : $defaults['video_filter_autoplay'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_related'] = array(
'#type' => 'radios',
'#title' => t('Related videos setting'),
'#description' => t('Show "related videos"? Not all video formats support this setting.'),
'#default_value' => isset($filter->settings['video_filter_related']) ? $filter->settings['video_filter_related'] : $defaults['video_filter_related'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_html5'] = array(
'#type' => 'radios',
'#title' => t('Use HTML5'),
'#description' => t('Use HTML5 if the codec provides it. Makes your videos more device agnostic.'),
'#default_value' => isset($filter->settings['video_filter_html5']) ? $filter->settings['video_filter_html5'] : $defaults['video_filter_html5'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_multiple_sources'] = array(
'#type' => 'radios',
'#title' => t('Allow multiple sources'),
'#description' => t('Allow the use of multiple sources (used source is selected at random).'),
'#default_value' => isset($filter->settings['video_filter_multiple_sources']) ? $filter->settings['video_filter_multiple_sources'] : $defaults['video_filter_multiple_sources'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
);
$settings['video_filter_codecs'] = array(
'#type' => 'checkboxes',
'#title' => t('Codecs'),
'#description' => t('Choose which codecs will be available.'),
'#default_value' => isset($filter->settings['video_filter_codecs']) ? $filter->settings['video_filter_codecs'] : $defaults['video_filter_codecs'],
'#options' => _video_filter_map_codecs_name(video_filter_get_codec_info()),
);
return $settings;
}
function _video_filter_map_codecs_name($codecs) {
$codecs_map = array();
foreach ($codecs as $codec_cod => $codec) {
$codecs_map[$codec_cod] = $codec['name'];
}
return $codecs_map;
}
function _video_filter_tips($filter, $format, $long = FALSE) {
if ($long) {
$codecs = video_filter_get_codec_enabled($filter->settings['video_filter_codecs']);
$supported = array();
$instructions = array();
foreach ($codecs as $codec) {
$supported[] = $codec['name'];
$instructions[] = isset($codec['instructions']) ? '
' . $codec['name'] . ':
' . $codec['instructions'] . '' : '';
}
return t('
Video Filter
You may insert videos from popular video sites by using a simple tag [video:URL]
.
Examples:
- Single video:
[video:http://www.youtube.com/watch?v=uN1qUeId]
- Random video out of multiple:
[video:http://www.youtube.com/watch?v=uN1qUeId1,http://www.youtube.com/watch?v=uN1qUeId2]
- Override default autoplay setting:
[video:http://www.youtube.com/watch?v=uN1qUeId autoplay:1]
- Override default width and height:
[video:http://www.youtube.com/watch?v=uN1qUeId width:X height:Y]
- Override default aspect ratio:
[video:http://www.youtube.com/watch?v=uN1qUeId ratio:4/3]
- Align the video:
[video:http://www.youtube.com/watch?v=uN1qUeId align:right]
Supported sites: @codecs.
Special instructions:
Some codecs need special input. You\'ll find those instructions here.
', array(
'@codecs' => implode(', ', $supported),
'!instructions' => implode('', $instructions),
)
);
}
else {
return t('You may insert videos with [video:URL]');
}
}
function _video_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
if (preg_match_all('/\[video(\:(.+))?( .+)?\]/isU', $text, $matches_code)) {
foreach ($matches_code[0] as $ci => $code) {
$video = array(
'source' => $matches_code[2][$ci],
'autoplay' => $filter->settings['video_filter_autoplay'],
'related' => $filter->settings['video_filter_related'],
);
// Pick random out of multiple sources separated by comma (,).
if ($filter->settings['video_filter_multiple_sources'] && strstr($video['source'], ',')) {
$sources = explode(',', $video['source']);
$random = array_rand($sources, 1);
$video['source'] = $sources[$random];
}
// Load all codecs.
$codecs = video_filter_get_codec_enabled($filter->settings['video_filter_codecs']);
// Find codec.
foreach ($codecs as $codec_name => $codec) {
if (!is_array($codec['regexp'])) {
$codec['regexp'] = array($codec['regexp']);
}
// Try different regular expressions.
foreach ($codec['regexp'] as $delta => $regexp) {
if (preg_match($regexp, $video['source'], $matches)) {
$video['codec'] = $codec;
$video['codec']['delta'] = $delta;
$video['codec']['matches'] = $matches;
// Used in theme function:
$video['codec']['codec_name'] = $codec_name;
break 2;
}
}
}
// Codec found.
if (isset($video['codec'])) {
// Override default attributes.
if ($matches_code[3][$ci] && preg_match_all('/\s+([a-zA-Z_]+)\:(\s+)?([0-9a-zA-Z\/]+)/i', $matches_code[3][$ci], $matches_attributes)) {
foreach ($matches_attributes[0] as $ai => $attribute) {
$video[$matches_attributes[1][$ai]] = $matches_attributes[3][$ai];
}
}
// Use configured ratio if present, otherwise use that from the codec,
// if set. Fall back to 1.
$ratio = 1;
if (isset($video['ratio']) && preg_match('/(\d+)\/(\d+)/', $video['ratio'], $tratio)) {
// Validate given ratio parameter.
$ratio = $tratio[1] / $tratio[2];
}
elseif (isset($video['codec']['ratio'])) {
if (is_float($video['codec']['ratio']) || is_int($video['codec']['ratio'])) {
$ratio = $video['codec']['ratio'];
}
elseif (preg_match('/(\d+)\s*\/\s*(\d+)/', $video['codec']['ratio'], $cratio)) {
$ratio = $cratio[1] / $cratio[2];
}
}
// Sets video width & height after any user input has been parsed.
// First, check if user has set a width.
if (isset($video['width']) && !isset($video['height'])) {
if ($ratio) {
$video['height'] = ceil($video['width'] / $ratio);
}
else {
$video['height'] = $filter->settings['video_filter_height'];
}
}
// Else, if user has set height.
elseif (isset($video['height']) && !isset($video['width'])) {
if ($ratio) {
$video['width'] = ceil($video['height'] * $ratio);
}
else {
$video['width'] = $filter->settings['video_filter_height'];
}
}
// Maybe both?
elseif (isset($video['height']) && isset($video['width'])) {
$video['width'] = $video['width'];
$video['height'] = $video['height'];
}
// Fall back to defaults.
elseif (!isset($video['height']) && !isset($video['width'])) {
$video['width'] = $filter->settings['video_filter_width'] != '' ? $filter->settings['video_filter_width'] : 400;
$video['height'] = $filter->settings['video_filter_height'] != '' ? $filter->settings['video_filter_height'] : 400;
}
// Default value for control bar height.
$control_bar_height = 0;
if (isset($video['control_bar_height'])) {
// Respect control_bar_height option if present.
$control_bar_height = $video['control_bar_height'];
}
elseif (isset($video['codec']['control_bar_height'])) {
// Respect setting provided by codec otherwise.
$control_bar_height = $video['codec']['control_bar_height'];
}
$video['height'] += $control_bar_height;
$video['autoplay'] = (bool) $video['autoplay'];
$video['align'] = (isset($video['align']) && in_array($video['align'], array(
'left',
'right',
'center',
))) ? $video['align'] : NULL;
// Let modules have final say on video parameters.
drupal_alter('video_filter_video', $video);
if (isset($video['codec']['html5_callback']) && $filter->settings['video_filter_html5'] == 1 && is_callable($video['codec']['html5_callback'], FALSE)) {
$replacement = call_user_func($video['codec']['html5_callback'], $video);
}
elseif (is_callable($video['codec']['callback'], FALSE)) {
$replacement = call_user_func($video['codec']['callback'], $video);
}
else {
// Invalid callback.
$replacement = '';
}
}
// Invalid format.
else {
$replacement = '';
}
$text = str_replace($code, $replacement, $text);
}
}
return $text;
}
/**
* Wrapper that calls the theme function.
*/
function video_filter_flash($video, $params = array()) {
return theme('video_filter_flash', array('video' => $video, 'params' => $params));
}
/**
* Wrapper that calls the theme function.
*/
function video_filter_iframe($video) {
return theme('video_filter_iframe', array('video' => $video));
}
/**
* Get a list of all available video codecs.
*/
function video_filter_get_codec_info() {
static $codecs;
if (!isset($codecs)) {
$codecs = module_invoke_all('codec_info');
drupal_alter('video_filter_codec_info', $codecs);
}
return $codecs;
}
/**
*
*/
function _video_filter_merge_format_codecs($filters_codecs) {
$codecs = array_pop($filters_codecs);
foreach ($filters_codecs as $format_name => $format_codecs) {
foreach ($format_codecs as $codec_name => $codec_value) {
if (!empty($codec_value) && empty($codecs[$codec_name])) {
$codecs[$codec_name] = $codec_value;
}
}
}
return $codecs;
}
/**
* Get a list of enabled video codecs.
*/
function video_filter_get_codec_enabled($video_filter_codecs) {
$codecs = array_intersect_key(
video_filter_get_codec_info(),
array_filter($video_filter_codecs)
);
return $codecs;
}
/**
* Function that outputs the