You are here

function oembed_file_formatter_view in oEmbed 7

Same name and namespace in other branches
  1. 8 oembed.module \oembed_file_formatter_view()
  2. 7.0 oembed.module \oembed_file_formatter_view()

Implements hook_file_formatter_FORMATTER_view().

1 string reference to 'oembed_file_formatter_view'
oembed_file_formatter_info in ./oembed.file.inc
Implements hook_file_formatter_info().

File

./oembed.module, line 618
Core functionality for oEmbed

Code

function oembed_file_formatter_view($file, $display, $langcode) {
  $scheme = file_uri_scheme($file->uri);
  if ($scheme == 'oembed') {

    /** @var \DrupalStreamWrapperInterface $wrapper */
    $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri);

    // Build render attributes array. Prefer file-specific overrides to display settings.
    $attributes = (isset($file->override) ? $file->override : array()) + $display['settings'];
    unset($attributes['attributes']);
    unset($attributes['wmode']);
    $parameters = array();
    if (!empty($display['settings']['wmode'])) {
      $parameters['mode'] = $display['settings']['wmode'];
    }

    // The oEmbed spec defines `maxwidth` and `maxheight` parameters, but some providers
    // support `width` and `height`. Precise dimensions supercede maximums.
    if ($file->type != 'image' && $display['type'] != 'oembed_thumbnail') {
      if (isset($attributes['width'])) {
        $parameters['maxwidth'] = $parameters['width'] = $attributes['width'];
      }
      if (isset($attributes['height'])) {
        $parameters['maxheight'] = $parameters['height'] = $attributes['height'];
      }
    }
    $element = oembed_render_element($display['type'], $wrapper
      ->getExternalUrl(), $parameters);
    $element['#attributes'] = $attributes;

    // Unfortunately, it's necessary to validate the oEmbed response before rendering
    // so that file_view_file() can continue to the next formatter.
    $output = drupal_render($element);
    if ($output) {
      return show($element);
    }
  }
}