charts_highcharts.install in Charts 5.0.x
Installation and update hooks for the Charts Highcharts module.
File
modules/charts_highcharts/charts_highcharts.installView source
<?php
/**
* @file
* Installation and update hooks for the Charts Highcharts module.
*/
use Drupal\charts\Settings\ChartsDefaultSettings;
/**
* Implements hook_requirements().
*/
function charts_highcharts_requirements($phase) {
$requirements = [];
switch ($phase) {
case 'runtime':
$library_path = charts_highcharts_find_library();
if (!$library_path) {
$requirements['charts_highcharts_js'] = [
'title' => t('Highcharts Library'),
'value' => t('Not Installed'),
'severity' => REQUIREMENT_ERROR,
'description' => t('You are missing the Highcharts library in your Drupal installation directory. Please see the README file inside charts_highcharts for instructions to install the library.'),
];
}
else {
$requirements['charts_highcharts_js'] = [
'title' => t('Highcharts Library'),
'severity' => REQUIREMENT_OK,
'value' => t('Installed'),
];
}
break;
}
if (function_exists('libraries_detect') && ($highcharts_info = libraries_detect('highcharts'))) {
if (is_dir($highcharts_info['library path'] . '/js/exporting-server')) {
$requirements['highcharts_security'] = [
'title' => t('Highcharts vulnerability'),
'severity' => REQUIREMENT_ERROR,
'value' => t('Dangerous sample code present'),
'description' => t('Your installation of the Highcharts library at "@path" contains a directory named "exporting-server". This directory contains dangerous sample files that may compromise the security of your site. You must delete this directory before you may use the Charts Highcharts module.', [
'@path' => $highcharts_info['library path'],
]),
];
}
}
return $requirements;
}
/**
* Get the location of the Highcharts library.
*
* @return string
* The location of the library, or FALSE if the library isn't installed.
*/
function charts_highcharts_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 . '/highcharts/highcharts.js')) {
return $dir . '/highcharts';
}
}
return FALSE;
}
/**
* Move the highcharts config to the main settings and plugin configuration.
*/
function charts_highcharts_update_8400() {
/** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
$config_factory = \Drupal::service('config.factory');
$highcharts_config = $config_factory
->getEditable('charts_highcharts.settings');
$config_keys = [
'legend_layout',
'legend_background_color',
'legend_border_width',
'legend_shadow',
'item_style_color',
'text_overflow',
];
$legend_configuration = [];
foreach ($config_keys as $key) {
if ($config_value = $highcharts_config
->get($key)) {
$config_value = ChartsDefaultSettings::transformBoolStringValueToBool($config_value);
if (substr($key, 0, 6) === 'legend') {
// Stripping legend_.
$new_key_map = substr($key, 7, strlen($key));
$legend_configuration[$new_key_map] = $config_value;
}
elseif ($key === 'item_style_color') {
$legend_configuration['item_style']['color'] = $config_value;
}
elseif ($key === 'text_overflow') {
$config_value = $config_value ?: '';
$legend_configuration['item_style']['text_overflow'] = $config_value;
}
}
}
if ($legend_configuration) {
$library_form = 'highcharts_settings';
// Get the main settings.
$charts_config = $config_factory
->getEditable('charts.settings');
$settings = $charts_config
->get('charts_default_settings') ?: [];
// Add the plugin configuration as part of it.
$settings[$library_form]['legend'] = $legend_configuration;
// Update the main config.
$charts_config
->set('charts_default_settings', $settings)
->save();
// Delete the highcharts configuration.
$highcharts_config
->delete();
}
}
Functions
Name | Description |
---|---|
charts_highcharts_find_library | Get the location of the Highcharts library. |
charts_highcharts_requirements | Implements hook_requirements(). |
charts_highcharts_update_8400 | Move the highcharts config to the main settings and plugin configuration. |