You are here

function media_token_to_markup in D7 Media 7

Replace callback to convert a media file tag into HTML markup.

Parameters

string $match: Takes a match of tag code

boolean $wysiwyg: Set to TRUE if called from within the WYSIWYG text area editor.

Return value

The HTML markup representation of the tag, or an empty string on failure.

See also

media_get_file_without_label()

hook_media_token_to_markup_alter()

1 call to media_token_to_markup()
_media_generate_tagMap in includes/media.filter.inc
Generates an array of [inline tags] => <html> to be used in filter replacement and to add the mapping to JS.
1 string reference to 'media_token_to_markup'
media_filter in includes/media.filter.inc
Filter callback for media markup filter.

File

includes/media.filter.inc, line 332
Functions related to the WYSIWYG editor and the media input filter.

Code

function 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');
    }
    $tag_info['file'] = $file;

    // The class attributes is a string, but drupal requires it to be
    // an array, so we fix it here.
    if (!empty($tag_info['attributes']['class'])) {
      $tag_info['attributes']['class'] = explode(" ", $tag_info['attributes']['class']);
    }

    // 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 '';
  }
  $element = media_get_file_without_label($file, $tag_info['view_mode'], $settings);
  drupal_alter('media_token_to_markup', $element, $tag_info, $settings);
  return drupal_render($element);
}