You are here

function formatter_suite_theme in Formatter Suite 8

Implements hook_theme().

File

./formatter_suite.module, line 26
Implements the principal entry points and hooks for the module.

Code

function formatter_suite_theme() {
  $themes = [];

  //
  // Field list theme.
  // -----------------
  // Copy the generic field theme to be a field list.
  $commonThemes = drupal_common_theme();
  $themes['formatter_suite_field_list'] = $commonThemes['field'];

  //
  // General image theme.
  // --------------------
  // The general image formatter swaps out the Image module's
  // 'image_formatter' for its own that uses a slightly better theme
  // that includes URL attributes.
  $themes['formatter_suite_general_image_formatter'] = [
    'variables' => [
      'item' => NULL,
      'item_attributes' => NULL,
      'url' => NULL,
      'attributes' => NULL,
      'image_style' => NULL,
    ],
  ];

  //
  // Image embed themes.
  // -------------------
  // The image embedding formatter needs to define three themes that
  // mimic standard Drupal themes when the formatter is used:
  //
  // - 'formatter_suite_image' mimics the Drupal core 'image' theme.
  //
  // - 'formatter_suite_image_style' mimics the image module's
  //   'image_style' theme.
  //
  // - 'formatter_suite_image_formatter' mimics the image module's
  //   'image_formatter' theme.
  //
  // This module defines TWIG templates that are copies of the original
  // themes so that we get the same layout.
  //
  // Since we want to mimic the original themes, we need the same set of
  // variables. These variable definitions are copied from the originals,
  // then augmented with additional variables needed for embedding.
  //
  // Get theme descriptions from Drupal core and the image module.
  $commonThemes = drupal_common_theme();
  $imageModuleThemes = image_theme();

  // Image embed themes.
  //
  // Copy theme variable definitions, then add variables for the minimum
  // and maximum image embed width.
  $themes['formatter_suite_image_embed'] = $commonThemes['image'];
  $themes['formatter_suite_image_embed_style'] = $imageModuleThemes['image_style'];
  $themes['formatter_suite_image_embed_formatter'] = $imageModuleThemes['image_formatter'];
  foreach ([
    'image_embed',
    'image_embed_style',
    'image_embed_formatter',
  ] as $key) {
    $th = 'formatter_suite_' . $key;
    $themes[$th]['variables']['maximumEmbedWidth'] = 0;
    $themes[$th]['variables']['maximumEmbedHeight'] = 0;
  }

  //
  // Patch image formatter-based themes.
  // -----------------------------------
  // Above we've copied the Image module's 'image_formatter' theme
  // description. Unfortunately, this includes a 'file' field that directs
  // the theme manager to include the Image module's 'image.field.inc'
  // where the template's preprocessor function lives. The theme manager
  // always assumes that the path in a 'file' field is relative to the
  // module involved, which is us, not the Image module. This causes the
  // Image module's 'image.field.inc' to not be found, which means its
  // preprocess function isn't found.
  //
  // The fix is this:
  //
  // 1. Remove the 'file' field from the theme description.
  // 2. Handle template preprocessing ourselves.
  unset($themes['formatter_suite_image_embed_formatter']['file']);
  return $themes;
}