You are here

syntaxhighlighter.install in Syntax Highlighter 8

Syntax Highlighter module installation file.

File

syntaxhighlighter.install
View source
<?php

/**
 * @file
 * Syntax Highlighter module installation file.
 */
use Drupal\Core\Render\Markup;
use Drupal\Core\Url;

/**
 * Implements hook_install().
 */
function syntaxhighlighter_install() {
  $message = t('You must <a href=":link">turn on the Syntax Highlighter filter</a> in an input format to syntax highlight code when using that format.', [
    ':link' => Url::FromRoute('filter.admin_overview')
      ->toString(),
  ]);
  $message .= ' ' . t('Then <a href=":link">configure the Syntax Highlighter module</a>.', [
    ':link' => Url::FromRoute('syntaxhighlighter.settings.form')
      ->toString(),
  ]);
  $message .= ' ' . t('See <a href=":link">the Syntax Highlighter module README.txt file</a> for instructions.', [
    ':link' => Url::FromUri('internal:/' . drupal_get_path('module', 'syntaxhighlighter') . '/README.txt')
      ->toString(),
  ]);
  drupal_set_message(Markup::create($message), 'status');

  // Setting up the autoloader script at install time covers the case of an
  // installation profile which sets 'use_autoloader' to true.
  $config = \Drupal::config('syntaxhighlighter.settings');
  _syntaxhighlighter_setup_autoloader_script($config);
}

/**
 * Implements hook_uninstall().
 */
function syntaxhighlighter_uninstall() {
  file_unmanaged_delete('public://js/syntaxhighlighter.autoloader.js');
  @\Drupal::service('file_system')
    ->rmdir('public://js');
}

/**
 * Implements hook_requirements().
 *
 * Make sure the user has installed the SyntaxHighlighter library.
 */
function syntaxhighlighter_requirements($phase) {
  $requirements = [];
  if ($phase == 'install') {

    // Make sure the libraries function is available during installation phase.
    if (!function_exists('libraries_get_path')) {

      /*
       * Make sure the libraries module has been registered. This covers the
       * case when syntaxhighlighter_requirements() is run from a custom
       * installation profile which depends on syntaxhighlighter.
       */
      if (!Drupal::moduleHandler()
        ->moduleExists('libraries')) {
        system_rebuild_module_data();
      }
      module_load_include('module', 'libraries');
    }
  }
  $lib_location = libraries_get_path('syntaxhighlighter');
  if (!$lib_location) {
    $requirements['syntaxhighlighter']['severity'] = REQUIREMENT_ERROR;
    $requirements['syntaxhighlighter']['description'] = t('The required SyntaxHighlighter Javascript library is not installed. See <a href=":link">the Syntax Highlighter module README.txt file</a> for instructions.', [
      ':link' => Url::FromUri('internal:/' . drupal_get_path('module', 'syntaxhighlighter') . '/README.txt')
        ->toString(),
    ]);
  }
  if ($phase === 'runtime') {
    $requirements['syntaxhighlighter']['title'] = t('Syntax Highlighter');
    $library = libraries_detect('syntaxhighlighter');
    if (!$library || !$library['installed']) {
      $requirements['syntaxhighlighter']['value'] = t('Not installed');
    }
    else {
      $requirements['syntaxhighlighter']['value'] = !empty($library['version']) ? $library['version'] : t('Installed');
    }
  }
  return $requirements;
}