You are here

flipping_book.module in Flipping Book 8

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

File

flipping_book.module
View source
<?php

/**
 * @file
 * Contains flipping_book.module..
 */
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\file\Entity\File;
use Drupal\flipping_book\Entity\FlippingBookType;

/**
 * Implements hook_help().
 */
function flipping_book_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the flipping_book module.
    case 'help.page.flipping_book':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provides integration with FlippingBook packages.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_theme().
 */
function flipping_book_theme() {
  $theme = [];
  $theme['flipping_book'] = array(
    'render element' => 'elements',
    'file' => 'flipping_book.page.inc',
    'template' => 'flipping_book',
  );
  $theme['flipping_book_content_add_list'] = [
    'render element' => 'content',
    'variables' => [
      'content' => NULL,
    ],
    'file' => 'flipping_book.page.inc',
  ];
  return $theme;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function flipping_book_theme_suggestions_flipping_book(array $variables) {
  $suggestions = array();
  $entity = $variables['elements']['#flipping_book'];
  $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
  $suggestions[] = 'flipping_book__' . $sanitized_view_mode;
  $suggestions[] = 'flipping_book__' . $entity
    ->bundle();
  $suggestions[] = 'flipping_book__' . $entity
    ->bundle() . '__' . $sanitized_view_mode;
  $suggestions[] = 'flipping_book__' . $entity
    ->id();
  $suggestions[] = 'flipping_book__' . $entity
    ->id() . '__' . $sanitized_view_mode;
  return $suggestions;
}

/**
 * Implements hook_flipping_book_delete().
 */
function flipping_book_flipping_book_delete(EntityInterface $entity) {
  $flipping_book = \Drupal::service('flipping_book');
  $flipping_book
    ->deleteArchive($entity);
}

/**
 * Implements hook_file_download().
 */
function flipping_book_file_download($uri) {
  if (!preg_match('|^([\\w\\-]+)://flipping_books/(.*?)/|', $uri, $matches)) {
    return NULL;
  }
  $storage = \Drupal::entityTypeManager()
    ->getStorage('flipping_book');
  $flipping_books = $storage
    ->loadByProperties([
    'directory' => $matches[2],
  ]);
  $flipping_book = reset($flipping_books);

  // Invalid flipping book uri provided.
  if (empty($flipping_book)) {
    return -1;
  }
  $fb_service = \Drupal::service('flipping_book');

  /** @var FlippingBookType $fb_type */
  $fb_type = $fb_service
    ->getBundleEntity($flipping_book);
  $file_system = \Drupal::service('file_system');
  $current_user = \Drupal::currentUser();
  $file_path = $file_system
    ->realpath($uri);
  $denied = !file_exists($file_path) || !is_file($file_path) || !$current_user
    ->hasPermission($fb_type
    ->getPermissionName());

  // Access specifically denied.
  if ($denied) {
    return -1;
  }
  $mime_type = \Drupal::service('file.mime_type.guesser');
  $uri_chunks = explode('/', $uri);
  $file = File::create();
  $file
    ->setFilename(end($uri_chunks));
  $file
    ->setMimeType($mime_type
    ->guess($uri));
  $file
    ->setSize(filesize($file_path));

  // Access is granted.
  $headers = file_get_content_headers($file);
  return $headers;
}