You are here

charts_highcharts.install in Charts 8.4

Installation and update hooks for the Charts Highcharts module.

File

modules/charts_highcharts/charts_highcharts.install
View 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 = [];
  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;
}

/**
 * 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

Namesort descending Description
charts_highcharts_requirements Implements hook_requirements().
charts_highcharts_update_8400 Move the highcharts config to the main settings and plugin configuration.