You are here

fotorama_gallery.install in Fotorama Gallery 8

Same filename and directory in other branches
  1. 8.2 fotorama_gallery.install

File

fotorama_gallery.install
View source
<?php

/**
 * Implements hook_install().
 */
function fotorama_gallery_install() {
  if (!_fotorama_gallery_verify_library()) {
    \Drupal::messenger()
      ->addError('Not Fotorama library found please install it, see README.md file for instructions.');
  }
}

/**
 * Implements hook_requirements().
 */
function fotorama_gallery_requirements($phase) {
  $requirements = [];
  if ($phase == 'runtime' && !_fotorama_gallery_verify_library()) {
    $requirements['fotorama_gallery_library'] = [
      'title' => t('Fotorama Library'),
      'description' => t('Fotorama Gallery module requires Fotorama library to works. Composer based install recommended, see README.md file for instructions.'),
      'severity' => REQUIREMENT_ERROR,
    ];
  }
  return $requirements;
}

/**
 * Verify that the library files exist.
 *
 * @param string $extension
 *   The name of the extension that registered a library.
 * @param string $name
 *   The name of a registered library to retrieve.
 *
 * @return bool
 *   TRUE if all files of this library exists, FALSE otherwise
 *
 * @see https://drupal.org/node/2231385
 */
function _fotorama_gallery_verify_library() {

  /** @var Drupal\Core\Asset\LibraryDiscovery $library_discovery */
  $library_discovery = \Drupal::service('library.discovery');
  $library = $library_discovery
    ->getLibraryByName('fotorama_gallery', 'fotorama');
  $exist = TRUE;
  if ($library['js']) {
    foreach ($library['js'] as $js) {
      if ($js['type'] == 'file') {
        if (!file_exists(DRUPAL_ROOT . '/' . $js['data'])) {
          $exist = FALSE;
        }
      }
    }
  }
  if ($library['css']) {
    foreach ($library['css'] as $css) {
      if ($css['type'] == 'file') {
        if (!file_exists(DRUPAL_ROOT . '/' . $css['data'])) {
          $exist = FALSE;
        }
      }
    }
  }
  return $exist;
}

Functions

Namesort descending Description
fotorama_gallery_install Implements hook_install().
fotorama_gallery_requirements Implements hook_requirements().
_fotorama_gallery_verify_library Verify that the library files exist.