Commit 60e26ff0 authored by Nia Kathoni's avatar Nia Kathoni Committed by Daniel Cothran
Browse files

Issue #3300070 by nikathone, srdtwc, andileco: Default colors not applying

parent 7ce1e976
Loading
Loading
Loading
Loading
+4 −0
Changes for src/Element/BaseSettings.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -1249,12 +1249,16 @@ class BaseSettings extends FormElement {
      ];
    }

    if (!empty($element['#default_value'])) {
      $options = NestedArray::mergeDeep($options, $element['#default_value']);
    }
    $element['series'] = [
      '#type' => 'chart_data_collector_table',
      '#initial_rows' => $element['#table_initial_rows'] ?? 5,
      '#initial_columns' => $element['#table_initial_columns'] ?? 2,
      '#table_drag' => FALSE,
      '#default_value' => $options['series'] ?? [],
      '#default_colors' => $options['display']['colors'] ?? [],
    ];

    return $element;
+29 −21
Changes for src/Element/ChartDataCollectorTable.php: 29 added lines, 21 removed lines.
Original line number Diff line number Diff line
@@ -44,6 +44,7 @@ class ChartDataCollectorTable extends FormElement {
      '#table_attributes' => [],
      // Allows to toggle on/off drupal tabledrag functionality.
      '#table_drag' => TRUE,
      '#default_colors' => [],
      '#process' => [
        [$class, 'processDataCollectorTable'],
      ],
@@ -85,7 +86,7 @@ class ChartDataCollectorTable extends FormElement {
      self::setElementState($parents, $form_state, $element_state);
    }
    else {
      // This is hack to make ajax call retun the proper identifier.
      // This is hack to make ajax call return the proper identifier.
      $element_state['table_categories_identifier'] = $value['table_categories_identifier'];
    }

@@ -138,7 +139,7 @@ class ChartDataCollectorTable extends FormElement {
      ];
    }

    if ($element['#table_wrapper'] && $element['#table_wrapper'] === 'container') {
    if ($element['#table_wrapper'] === 'container') {
      $element['table_wrapper'] = [
        '#type' => 'container',
        '#attributes' => $element['#table_wrapper_attributes'],
@@ -193,6 +194,10 @@ class ChartDataCollectorTable extends FormElement {
        ];

        if (!$is_category_cell && ($add_color_first_row || (!$is_first_column && $j === $first_col_key))) {
          if (empty($column['color'])) {
            $color_index = $is_first_column ? $j : $i;
            $column['color'] = $element['#default_colors'][$color_index - 1] ?? self::randomColor();
          }
          $row_form[$j]['#wrapper_attributes'] = [
            'class' => ['container-inline'],
          ];
@@ -203,7 +208,7 @@ class ChartDataCollectorTable extends FormElement {
            '#attributes' => ['TYPE' => 'color'],
            '#size' => 10,
            '#maxlength' => 7,
            '#default_value' => $column['color'] ?? self::randomColor(),
            '#default_value' => $column['color'],
          ];
        }
      }
@@ -285,7 +290,7 @@ class ChartDataCollectorTable extends FormElement {

    // Footer operations.
    $table['_operations'] = [
      '#attributes' => ['class' => ['data-collector-table--oprations-row']],
      '#attributes' => ['class' => ['data-collector-table--operations-row']],
    ];
    $table['_operations']['wrapper'] = [
      '#type' => 'container',
@@ -367,7 +372,7 @@ class ChartDataCollectorTable extends FormElement {
    $form_state->setValue($parents, $value);

    if ($element['#required'] && empty($value['table_categories_identifier'])) {
      $form_state->setError($element['table_categories_identifier'], t('Please select how categories should be identiefied.'));
      $form_state->setError($element['table_categories_identifier'], t('Please select how categories should be identified.'));
    }
  }

@@ -479,7 +484,7 @@ class ChartDataCollectorTable extends FormElement {
   *
   * Operation.
   */
  private static function buildOperationButton($operation, $on, $id_prefix, $wrapper_id, $index = NULL, $attributes = [], $wrapper_atrributes = []) {
  private static function buildOperationButton($operation, $on, $id_prefix, $wrapper_id, $index = NULL, $attributes = [], $wrapper_attributes = []) {
    $name = $id_prefix . '_' . $operation . '_' . $on;
    $submit = [];

@@ -492,8 +497,8 @@ class ChartDataCollectorTable extends FormElement {
      $submit['#attributes'] = $attributes;
    }

    if ($wrapper_atrributes) {
      $submit['#wrapper_attributes'] = $wrapper_atrributes;
    if ($wrapper_attributes) {
      $submit['#wrapper_attributes'] = $wrapper_attributes;
    }

    $submit += [
@@ -528,7 +533,7 @@ class ChartDataCollectorTable extends FormElement {
   * @return array
   *   The element state storage.
   */
  private static function initializeEmptyTable(array $element, $identifier_value) {
  private static function initializeEmptyTable(array $element, string $identifier_value) {
    $is_first_column = $identifier_value === self::FIRST_COLUMN;
    $columns = $element['#initial_columns'];
    $columns_arr = range(0, $columns - 1);
@@ -537,6 +542,8 @@ class ChartDataCollectorTable extends FormElement {

    $data = [];
    $first_row_key = NULL;
    $counter_default_used_color_index = 0;
    $max_default_colors = count($element['#default_colors']);
    foreach ($rows_arr as $i) {
      $first_row_key = $first_row_key === NULL ? $i : $first_row_key;
      $table_first_row = $i === $first_row_key;
@@ -548,7 +555,11 @@ class ChartDataCollectorTable extends FormElement {
        $is_category_cell = $table_first_col && $table_first_row;
        $data[$i][$j]['data'] = '';
        if (!$is_category_cell && (($is_first_column && $i === $first_row_key) || (!$is_first_column && $j === $first_col_key))) {
          $data[$i][$j]['color'] = self::randomColor();
          if ($counter_default_used_color_index === $max_default_colors) {
            $counter_default_used_color_index = 0;
          }
          $data[$i][$j]['color'] = $element['#default_colors'][$counter_default_used_color_index] ?? self::randomColor();
          $counter_default_used_color_index++;
        }
      }
    }
@@ -685,7 +696,7 @@ class ChartDataCollectorTable extends FormElement {
      return $data;
    }

    // Try convert the data to UTF-8.
    // Try to convert the data to UTF-8.
    if ($encoded_data = Unicode::convertToUtf8($data, $encoding)) {
      return $encoded_data;
    }
@@ -705,13 +716,10 @@ class ChartDataCollectorTable extends FormElement {
   * @return array
   *   The category label and data.
   */
  public static function getCategoriesFromCollectedTable(array $data, $type) {
  public static function getCategoriesFromCollectedTable(array $data, string $type) {
    $categories_identifier = $data['table_categories_identifier'];
    $table = $data['data_collector_table'];
    $categories = [
      'label' => '',
      'data' => [],
    ];
    $categories = [];

    $is_first_column = $categories_identifier === self::FIRST_COLUMN;
    $first_row = current($table);
@@ -760,7 +768,7 @@ class ChartDataCollectorTable extends FormElement {
   * @return array
   *   The series.
   */
  public static function getSeriesFromCollectedTable(array $data, $type) {
  public static function getSeriesFromCollectedTable(array $data, string $type) {
    $table = $data['data_collector_table'];
    $categories_identifier = $data['table_categories_identifier'];

@@ -783,11 +791,11 @@ class ChartDataCollectorTable extends FormElement {
    foreach ($table as $row_key => $row) {
      if (!$is_first_column) {
        $name_key = key($row);
        $series[$i]['name'] = $row[$name_key]['data'];
        $series[$i]['color'] = $row[$name_key]['color'];
        $series[$i]['name'] = $row[$name_key]['data'] ?? [];
        $series[$i]['color'] = $row[$name_key]['color'] ?? '';
        // Removing the name from data array.
        unset($row[$name_key]);
        foreach (array_values($row) as $column) {
        foreach ($row as $column) {
          // Get all the data in this column and break out of this loop.
          if ($is_single_axis) {
            if (is_numeric($column) || is_string($column)) {
@@ -821,7 +829,7 @@ class ChartDataCollectorTable extends FormElement {
            continue;
          }
          elseif ($i === 0) {
            // This is the first row which holds the data names.
            // This is the first column which holds the data names and colors.
            $series[$j]['name'] = $column['data'] ?? $column;
            $series[$j]['color'] = $column['color'] ?? self::randomColor();
          }
+1 −3
Changes for src/Plugin/Field/FieldType/ChartConfigItem.php: 1 added line, 3 removed lines.
Original line number Diff line number Diff line
@@ -46,7 +46,7 @@ class ChartConfigItem extends FieldItemBase {
   * {@inheritdoc}
   */
  public static function schema(FieldStorageDefinitionInterface $field_definition) {
    $schema = [
    return [
      'columns' => [
        'config' => [
          'type' => 'blob',
@@ -68,8 +68,6 @@ class ChartConfigItem extends FieldItemBase {
        ],
      ],
    ];

    return $schema;
  }

  /**
+2 −0
Changes for tests/modules/charts_test/src/Form/DataCollectorTableTestForm.php: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -26,11 +26,13 @@ class DataCollectorTableTestForm extends FormBase {
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('charts.settings');
    $form['series'] = [
      '#type' => 'chart_data_collector_table',
      '#initial_rows' => self::INITIAL_ROWS,
      '#initial_columns' => self::INITIAL_COLUMNS,
      '#table_drag' => FALSE,
      '#default_colors' => $config->get('charts_default_settings.display.colors') ?? [],
    ];

    return $form;
+24 −2
Changes for tests/src/FunctionalJavascript/DataCollectorTableTest.php: 24 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -104,6 +104,28 @@ class DataCollectorTableTest extends WebDriverTestBase {
    // Submit the form and verify the submitted data. - skip.
  }

  /**
   * Tests the default colors pn the "chart_data_collector_table" element.
   */
  public function testColorDefaultColors() {
    $chart_config = \Drupal::config('charts.settings');
    $default_colors = $chart_config->get('charts_default_settings.display.colors');
    $this->drupalGet('/charts_test/data_collector_table_test_form');
    $page = $this->getSession()->getPage();

    // Get the first row, then inside the first row get the color input and check
    // its value.
    $first_row_color_input = $page->find('css', static::TABLE_ROW_SELECTOR . ':first-child td:nth-child(2) input[type="color"]');
    $this->assertEquals($default_colors[0], $first_row_color_input->getValue());

    // Adding one column.
    $this->doTableOperation('add', 'column');

    // Checking if the added column also has the expected color.
    $first_row_color_input = $page->find('css', static::TABLE_ROW_SELECTOR . ':first-child td:nth-child(3) input[type="color"]');
    $this->assertEquals($default_colors[1], $first_row_color_input->getValue());
  }

  /**
   * Do table operation.
   *
@@ -182,7 +204,7 @@ class DataCollectorTableTest extends WebDriverTestBase {
   * @param string $on
   *   The element.
   */
  protected function assertNewCellIsEmpty($on) {
  protected function assertNewCellIsEmpty(string $on) {
    $page = $this->getSession()->getPage();
    if ($on === 'row') {
      $counter = DataCollectorTableTestForm::INITIAL_ROWS + 1;
@@ -244,7 +266,7 @@ class DataCollectorTableTest extends WebDriverTestBase {
  }

  /**
   * Get url of ressources.
   * Get url of resources.
   *
   * @return \Drupal\Core\GeneratedUrl|string
   *   The url.