You are here

gdoc_field.module in Embedded Google Docs Viewer 8

Same filename and directory in other branches
  1. 7 gdoc_field.module

A field formatter for the File field.

This formatter 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 .doc/.docx, .xls/.xlsx. and .ppt/.pptx.

File

gdoc_field.module
View source
<?php

/**
 * @file
 * A field formatter for the File field.
 *
 * This formatter 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 .doc/.docx, .xls/.xlsx. and .ppt/.pptx.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function gdoc_field_help($route_name, RouteMatchInterface $route_match) {
  $helptext = '';
  if ($route_name == 'help.page.gdoc_field') {
    $helptext = '<p>';
    $helptext .= t('This module provides a new format for the File field type. This format presents the file as a fully rendered object within a web page - i.e. it displays the contents of the file as appropriate to its filetype (Adobe Acrobat .pdf, Microsoft Word .doc/.docx, Microsoft Excel .xls/.xlsx, Microsoft Powerpoint .ppt/.pptx), using the Google Docs embedded rendering engine.');
    $helptext .= '</p>';
    $helptext .= '<p>';
    $helptext .= t("N.B.: Only files that are public may use this formatter - Google Docs must be able to access the file in order to  render and display it. In other words, it won't work on a typical development laptop, or if your server is behind a firewall where Google is unable to access it.");
    $helptext .= '</p>';
    $helptext .= '<p>';
    $helptext .= t("To use this field format, add a File field to a new or existing content type (such as Basic Page) on the content type's Manage Fields form. The File field type provides only one widget type - File - so select that. On the content type's 'Manage Display' form, there will be a drop-down select list of available display formats for the File field. To display the file within the embedded Google Docs viewer, choose the 'Embedded Google Documents Viewer' format.");
    $helptext .= '</p>';
    $helptext .= '<p>';
    $helptext .= t("The document viewer is styled using the gdoc-field.html.twig template. Several theme suggestions are provided to further customize the display of this field. By default, the viewer's width is 80% with 10% margins on left and right, and its height is 400px, with a 1px solid black border.");
    $helptext .= '</p>';
  }
  return $helptext;
}

/**
 * Implements hook_theme().
 */
function gdoc_field_theme() {
  return [
    'gdoc_field' => [
      'variables' => [
        'url' => NULL,
        'filename' => NULL,
        'delta' => NULL,
        'entity' => NULL,
        'bundle' => NULL,
        'field_name' => NULL,
        'field_type' => NULL,
      ],
    ],
  ];
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function gdoc_field_theme_suggestions_gdoc_field(array $variables) {
  $entity = $variables['entity'];
  $bundle = $variables['bundle'];
  $delta = $variables['delta'];
  $field_name = $variables['field_name'];
  $field_type = $variables['field_type'];
  $suggestions[] = $variables['theme_hook_original'] . '__' . $entity;
  $suggestions[] = $variables['theme_hook_original'] . '__' . $entity . '__' . $bundle;
  $suggestions[] = $variables['theme_hook_original'] . '__' . $entity . '__' . $bundle . '__' . $delta;
  $suggestions[] = $variables['theme_hook_original'] . '__' . $field_type;
  $suggestions[] = $variables['theme_hook_original'] . '__' . $field_type . '__' . $field_name;
  $suggestions[] = $variables['theme_hook_original'] . '__' . $field_type . '__' . $field_name . '__' . $delta;
  return $suggestions;
}