You are here

function gdoc_field_field_formatter_view in Embedded Google Docs Viewer 7

Implements hook_field_formatter_view().

Renders the ouput of an 'Embedded Google Docs viewer' formatted field within an iframe that pulls in the Google Docs viewer to display the file inline.

File

./gdoc_field.module, line 71
A field formatter for the File field, which renders the file as an embedded document, using Google's Google Docs embeddable file viewer. At the current time this viewer will reasonably display files of these types: Adobe .pdf, Microsoft…

Code

function gdoc_field_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {

    // This formatter outputs the field within an iframe.
    case 'gdoc_field_embedded_doc':
      foreach ($items as $delta => $item) {
        $item_uri = $item['uri'];
        if (file_uri_scheme($item_uri) == 'public') {
          $url = file_create_url($item_uri);
          $encoded_url = urlencode($url);
          drupal_add_css(drupal_get_path('module', 'gdoc_field') . '/gdoc_field.css');
          $element[$delta]['#markup'] = '<iframe class="gdoc-field" src="//docs.google.com/gview?embedded=true&url=' . $encoded_url . '"></iframe>';
        }
        else {
          drupal_set_message(t('The file (%file) is not publicly accessable. It must be publicly available in order for the Google Docs viewer to be able to access it.', array(
            '%file' => $item['filename'],
          )), 'error', FALSE);
        }
      }
      break;
  }
  return $element;
}