charts-8.x-4.x-dev/modules/charts_chartjs/charts_chartjs.install
modules/charts_chartjs/charts_chartjs.install
<?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();
$config = \Drupal::config('charts.settings');
$cdn = $config->get('advanced.requirements.cdn') ?? FALSE;
if (!$library_path) {
if ($cdn) {
$requirements['charts_chartjs_js'] = [
'title' => t('Chart.js Library'),
'value' => t('Available through a CDN'),
'severity' => REQUIREMENT_WARNING,
'description' => t('You are using the Chart.js library via a content delivery network. It is generally considered better practice to install the library files locally. Please see the README file inside charts_chartjs for instructions to install the library.'),
];
}
else {
$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 and you have opted not to use a CDN. Please either enable use of the CDN in the Chart Settings under the Advanced tab or see the README file inside charts_chartjs for instructions to install the library.'),
];
}
}
else {
// If the library directory contains "auto", provide a warning.
if (file_exists($library_path . '/auto')) {
$requirements['charts_chartjs_js'] = [
'title' => t('Chart.js Library'),
'value' => t('Installed'),
'severity' => REQUIREMENT_WARNING,
'description' => t('You have installed the Chart.js library in your Drupal installation directory, but it contains additional files that are not required and may be harmful. Please delete everything except the files listed in charts_chartjs.libraries.yml. We have included a suggested post-install/post-update script in the README file.'),
];
}
else {
$requirements['charts_chartjs_js'] = [
'title' => t('Chart.js Library'),
'value' => t('Installed'),
'severity' => REQUIREMENT_OK,
'description' => t('You have installed the Chart.js library in your Drupal installation directory.'),
];
}
}
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()
$search_dir = [];
// Similar to 'modules' and 'themes' directories inside an installation
// profile, installation profiles may want to place libraries into a
// 'libraries' directory.
$search_dir[] = 'profiles/' . \Drupal::installProfile() . '/libraries';
// Always search libraries.
$search_dir[] = 'libraries';
// Also search sites/<domain>/*.
$container = \Drupal::getContainer();
$site_path = $container->getParameter('site.path');
$site_path = explode('/', $site_path);
$site_name = $site_path[1];
$search_dir[] = $site_name . '/libraries';
foreach ($search_dir as $dir) {
if (file_exists($dir . '/chart.js/dist/chart.js') || file_exists($dir . '/chart.js/dist/chart.umd.js')) {
return $dir . '/chartjs';
}
}
return FALSE;
}
