charts_echarts-1.0.0-alpha1/charts_echarts.install
charts_echarts.install
<?php
/**
* @file
* Installation and uninstallation functions.
*/
/**
* Implements hook_requirements().
*/
function charts_echarts_requirements($phase) {
$requirements = [];
switch ($phase) {
case 'runtime':
$library_path = charts_echarts_find_library();
$config = \Drupal::config('charts.settings');
$cdn = $config->get('advanced.requirements.cdn') ?? FALSE;
if (!$library_path) {
if ($cdn) {
$requirements['charts_echarts_js'] = [
'title' => t('ECharts Library'),
'value' => t('Available through a CDN'),
'severity' => REQUIREMENT_WARNING,
'description' => t('You are using the ECharts 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_echarts for instructions to install the library.'),
];
}
else {
$requirements['charts_echarts_js'] = [
'title' => t('ECharts Library'),
'value' => t('Not Installed'),
'severity' => REQUIREMENT_ERROR,
'description' => t('You are missing the ECharts 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_echarts for instructions to install the library.'),
];
}
}
else {
// If the library directory contains "auto", provide a warning.
if (file_exists($library_path . '/build')) {
$requirements['charts_echarts_js'] = [
'title' => t('ECharts Library'),
'value' => t('Installed'),
'severity' => REQUIREMENT_WARNING,
'description' => t('You have installed the ECharts 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_echarts.libraries.yml. We have included a suggested post-install/post-update script in the README file.'),
];
}
else {
$requirements['charts_echarts_js'] = [
'title' => t('ECharts Library'),
'value' => t('Installed'),
'severity' => REQUIREMENT_OK,
'description' => t('You have installed the ECharts library in your Drupal installation directory.'),
];
}
}
break;
}
return $requirements;
}
/**
* Get the location of the ECharts library.
*
* @return string
* The location of the library, or FALSE if the library isn't installed.
*/
function charts_echarts_find_library() {
$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>/*.
$container = \Drupal::getContainer();
$site_path = $container->getParameter('site.path');
$site_path = explode('/', $site_path);
$site_name = $site_path[1];
$searchdir[] = $site_name . '/libraries';
foreach ($searchdir as $dir) {
if (file_exists($dir . '/echarts/dist/echarts.min.js')) {
return $dir . '/echarts/dist';
}
}
return FALSE;
}
