Commit 17439fbe authored by Nia Kathoni's avatar Nia Kathoni Committed by Daniel Cothran
Browse files

Issue #3272237 by nikathone, andileco: Start with a higher number of default colors

parent 459b4ea0
Loading
Loading
Loading
Loading
+25 −10
Original line number Diff line number Diff line
@@ -4,16 +4,31 @@ charts_default_settings:
  display:
    title: ''
    colors:
      - '#d8810f'
      - '#0d233a'
      - '#8bbc21'
      - '#910000'
      - '#1aadce'
      - '#492970'
      - '#f28f43'
      - '#77a1e5'
      - '#c42525'
      - '#a6c96a'
      - '#006fb0'
      - '#f07c33'
      - '#342e9c'
      - '#579b17'
      - '#3f067a'
      - '#cbde67'
      - '#7643b6'
      - '#738d00'
      - '#c157c7'
      - '#02dab1'
      - '#ed56b4'
      - '#d8d981'
      - '#004695'
      - '#736000'
      - '#a5a5ff'
      - '#833a00'
      - '#ff9ee9'
      - '#684507'
      - '#fe4f85'
      - '#5d0011'
      - '#ffa67b'
      - '#88005c'
      - '#ff9b8f'
      - '#85000f'
      - '#ff7581'
    title_position: out
    tooltips: true
    tooltips_use_html: false
+14 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\charts\Element;

use Drupal\charts\Settings\ChartsDefaultColors;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityPublishedInterface;
@@ -867,10 +868,17 @@ class BaseSettings extends FormElement {
    // Taken from WidgetBase::formMultipleElements().
    $max_weight = count($field_options);

    $field_options_count = 0;
    $default_colors = (new ChartsDefaultColors())->getDefaultColors();

    foreach ($field_options as $field_name => $field_label) {
      $field_option_element = &$element['fields']['data_providers'][$field_name];
      $default_value = $options['fields']['data_providers'][$field_name] ?? [];
      $default_weight = $default_value['weight'] ?? $max_weight;
      $default_color = $default_value['color'] ?? '';
      if (!$default_color) {
        $default_color = $default_colors[$field_options_count] ?? '#000000';
      }

      $field_option_element['#attributes']['class'][] = 'draggable';
      // Field option label.
@@ -899,7 +907,7 @@ class BaseSettings extends FormElement {
        '#title_display' => 'invisible',
        '#size' => 10,
        '#maxlength' => 7,
        '#default_value' => $default_value['color'] ?? '#000000',
        '#default_value' => $default_color,
      ];

      $field_option_element['weight'] = [
@@ -914,6 +922,7 @@ class BaseSettings extends FormElement {
      ];

      $field_option_element['#weight'] = $default_weight;
      $field_options_count++;
    }

    $element['fields']['entity_grouping'] = [
@@ -1052,7 +1061,9 @@ class BaseSettings extends FormElement {
      '#suffix' => '</div>',
    ];

    for ($color_count = 0; $color_count < 10; $color_count++) {
    // Using the default colors in the settings to populate the colors.
    $default_colors = (new ChartsDefaultColors())->getDefaultColors();
    for ($color_count = 0; $color_count < count($default_colors); $color_count++) {
      $element['display']['colors'][$color_count] = [
        '#type' => 'textfield',
        '#attributes' => ['TYPE' => 'color'],
@@ -1060,7 +1071,7 @@ class BaseSettings extends FormElement {
        '#maxlength' => 7,
        '#theme_wrappers' => [],
        '#suffix' => ' ',
        '#default_value' => $options['display']['colors'][$color_count] ?? '',
        '#default_value' => $options['display']['colors'][$color_count] ?? $default_colors[$color_count],
      ];
    }

+17 −3
Original line number Diff line number Diff line
@@ -2,6 +2,8 @@

namespace Drupal\charts\Settings;

use Drupal\Core\Serialization\Yaml;

/**
 * Class ChartsDefaultColors.
 *
@@ -27,6 +29,11 @@ class ChartsDefaultColors {
    '#a6c96a',
  ];

  public function __construct() {
    $install_settings = $this->getDefaultInstallSettings();
    $this->defaultColors = $install_settings['charts_default_settings']['display']['colors'] ?? $this->defaultColors;
  }

  /**
   * Default defined colors.
   *
@@ -47,6 +54,16 @@ class ChartsDefaultColors {
    $this->defaultColors = $defaultColors;
  }

  private function getDefaultInstallSettings() {
    $path = \Drupal::service('extension.list.module')->getPath('charts');
    $default_settings_file = $path . '/config/install/charts.settings.yml';
    if (!file_exists($default_settings_file)) {
      return [];
    }

    return Yaml::decode(file_get_contents($default_settings_file));
  }

  /**
   * Provide a random color.
   *
@@ -57,7 +74,4 @@ class ChartsDefaultColors {
    return sprintf('#%06X', mt_rand(0, 0xFFFFFF));
  }

  // Private static function randomColorPart() {.
  // Return str_pad( dechex(mt_rand(0, 255)), 2, '0', STR_PAD_LEFT);
  // }.
}