Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created November 23, 2011 22:15
Show Gist options
  • Save bangpound/1390085 to your computer and use it in GitHub Desktop.
Save bangpound/1390085 to your computer and use it in GitHub Desktop.
override media filter to show full file entities not just file field
<?php
/**
* Implement hook_filter_info_alter().
*/
function whatsit_filter_info_alter(&$info) {
$info['media_filter']['process callback'] = 'whatsit_media_filter';
}
/**
* Filter callback for media markup filter.
*
* @TODO check for security probably pass text through filter_xss
* @return unknown_type
*/
function whatsit_media_filter($text) {
$text = ' ' . $text . ' ';
$text = preg_replace_callback("/\[\[.*?]]/s",'whatsit_media_token_to_markup', $text);
return $text;
}
/**
* Replace callback to convert tag into markup
* @param string $match
* Takes a match of tag code
* @param boolean $wysiwyg
* Set to TRUE if called from within the WYSIWYG text area editor.
* @return
* Return the replaced markup
*/
function whatsit_media_token_to_markup($match, $wysiwyg = FALSE) {
$settings = array();
$match = str_replace("[[","",$match);
$match = str_replace("]]","",$match);
$tag = $match[0];
try {
if (!is_string($tag)) {
throw new Exception('Unable to find matching tag');
}
$tag_info = drupal_json_decode($tag);
if (!isset($tag_info['fid'])) {
throw new Exception('No file Id');
}
if (!isset($tag_info['view_mode'])) {
// Should we log or throw an exception here instead?
// Do we need to validate the view mode for fields API?
$tag_info['view_mode'] = media_variable_get('wysiwyg_default_view_mode');
}
$file = file_load($tag_info['fid']);
if (!$file) {
throw new Exception('Could not load media object');
}
// Track the fid of this file in the {media_filter_usage} table.
media_filter_track_usage($file->fid);
$attributes = is_array($tag_info['attributes']) ? $tag_info['attributes'] : array();
$attribute_whitelist = media_variable_get('wysiwyg_allowed_attributes');
$settings['attributes'] = array_intersect_key($attributes, array_flip($attribute_whitelist));
// Many media formatters will want to apply width and height independently
// of the style attribute or the corresponding HTML attributes, so pull
// these two out into top-level settings. Different WYSIWYG editors have
// different behavior with respect to whether they store user-specified
// dimensions in the HTML attributes or the style attribute, so check both.
// Per http://www.w3.org/TR/html5/the-map-element.html#attr-dim-width, the
// HTML attributes are merely hints: CSS takes precedence.
if (isset($settings['attributes']['style'])) {
$css_properties = media_parse_css_declarations($settings['attributes']['style']);
foreach (array('width', 'height') as $dimension) {
if (isset($css_properties[$dimension]) && substr($css_properties[$dimension], -2) == 'px') {
$settings[$dimension] = substr($css_properties[$dimension], 0, -2);
}
elseif (isset($settings['attributes'][$dimension])) {
$settings[$dimension] = $settings['attributes'][$dimension];
}
}
}
if ($wysiwyg) {
$settings['wysiwyg'] = $wysiwyg;
}
}
catch (Exception $e) {
watchdog('media', 'Unable to render media from %tag. Error: %error', array('%tag' => $tag, '%error' => $e->getMessage()));
return '';
}
$file->override = $settings;
// Legacy support for Styles module plugins that expect overridden HTML
// attributes in $file->override rather than $file->override['attributes'].
if (isset($settings['attributes'])) {
$file->override += $settings['attributes'];
}
$element = file_view($file, $tag_info['view_mode']);
unset($settings['attributes']['alt']);
unset($settings['attributes']['title']);
// The formatter invoked by file_view_file() can use $file->override to
// customize the returned render array to match the requested settings. To
// support simple formatters that don't do this, set the element attributes to
// what was requested, but not if the formatter applied its own logic for
// element attributes.
if (!isset($element['#attributes']) && isset($settings['attributes'])) {
$element['#attributes'] = $settings['attributes'];
}
$element['#theme_wrappers'] = array('container');
return drupal_render($element);
}
@bangpound
Copy link
Author

whatsit_media_token_to_markup() is almost completely identical to media_token_to_markup() except it uses file_view() instead of just showing the file field itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment