You are here

charts_chartjs.install in Charts 5.0.x

Installation and uninstallation functions.

File

modules/charts_chartjs/charts_chartjs.install
View source
<?php

/**
 * @file
 * Installation and uninstallation functions.
 */

/**
 * Implements hook_requirements().
 */
function charts_chartjs_requirements($phase) {
  $requirements = [];
  switch ($phase) {
    case 'runtime':
      $library_path = charts_chartjs_find_library();
      if (!$library_path) {
        $requirements['charts_chartjs_js'] = [
          'title' => t('Chart.js Library'),
          'value' => t('Not Installed'),
          'severity' => REQUIREMENT_ERROR,
          'description' => t('You are missing the Chart.js library in your Drupal installation directory. Please see the README file inside charts_chartjs for instructions to install the library.'),
        ];
      }
      else {
        $requirements['charts_chartjs_js'] = [
          'title' => t('Chart.js Library'),
          'severity' => REQUIREMENT_OK,
          'value' => t('Installed'),
        ];
      }
      break;
  }
  return $requirements;
}

/**
 * Get the location of the Chart.js library.
 *
 * @return string
 *   The location of the library, or FALSE if the library isn't installed.
 */
function charts_chartjs_find_library() {

  // The following logic is taken from libraries_get_libraries()
  $searchdir = [];

  // Similar to 'modules' and 'themes' directories inside an installation
  // profile, installation profiles may want to place libraries into a
  // 'libraries' directory.
  $searchdir[] = 'profiles/' . \Drupal::installProfile() . '/libraries';

  // Always search libraries.
  $searchdir[] = 'libraries';

  // Also search sites/<domain>/*.
  $searchdir[] = \Drupal::service('site.path') . '/libraries';
  foreach ($searchdir as $dir) {
    if (file_exists($dir . '/chartjs/dist/chart.js')) {
      return $dir . '/chartjs';
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
charts_chartjs_find_library Get the location of the Chart.js library.
charts_chartjs_requirements Implements hook_requirements().