Skip to content

Instantly share code, notes, and snippets.

@davereid
Created June 29, 2012 19:48
Show Gist options
  • Save davereid/3020238 to your computer and use it in GitHub Desktop.
Save davereid/3020238 to your computer and use it in GitHub Desktop.
media_url_filter
<?php
// This code was removed from Media module as it was no longer used and does not work.
// Removed via http://drupal.org/node/1268268
/**
* Filter callback for media url filter.
* @TODO: There are currently problems with this. For instance, if a file is
* to be loaded from a remote location here, it will be recreated multiple
* times, each time this filter is called. If we want to continue supporting
* this feature, we would need to probably create a new stream or other way
* to lookup a remote file w/ its local version. Probably best as a contributed
* module because of this difficulty. ~ aaron.
*/
function media_url_filter($text, $filter) {
$text = ' ' . $text . ' ';
// Need to attach the variables to the callback after the regex.
$callback = _media_url_curry('_media_url_parse_full_links', 1);
// Match absolute URLs.
$text = preg_replace_callback("`(<p>|<li>|<br\s*/?>|[ \n\r\t\(])((http://|https://)([a-zA-Z0-9@:%_+*~#?&=.,/;-]*[a-zA-Z0-9@:%_+*~#&=/;-]))([.,?!]*?)(?=(</p>|</li>|<br\s*/?>|[ \n\r\t\)]))`i", $callback, $text);
return $text;
}
/**
* If one of our allowed providers knows what to do with the url,
* then let it embed the video.
*
* @param int $filter
* The filter id.
* @param array $match
* The matched text from our regex.
*
* @return string
* The replacement text for the url.
*/
function _media_url_parse_full_links($match) {
// Get just the URL.
$match[2] = check_url(decode_entities($match[2]));
try {
$file = media_parse_to_file($match[2]);
}
catch (Exception $e) {
// Ignore errors; pass the original text for other filters to deal with.
return $match[0];
}
if ($file->fid) {
$file = file_load($file->fid);
// Generate a preview of the file
// @TODO: Allow user to change the formatter in the filter settings.
$preview = file_view_file($file, 'media_large');
$preview['#show_names'] = TRUE;
return drupal_render($preview);
}
// Nothing was parsed; return the original text.
return $match[0];
}
function _media_url_curry($func, $arity) {
return create_function('', "
\$args = func_get_args();
if(count(\$args) >= $arity)
return call_user_func_array('$func', \$args);
\$args = var_export(\$args, 1);
return create_function('','
\$a = func_get_args();
\$z = ' . \$args . ';
\$a = array_merge(\$z,\$a);
return call_user_func_array(\'$func\', \$a);
');
");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment