From 9f8abe6d5da395033f76b3daa63af4ee2ce8d737 Mon Sep 17 00:00:00 2001
From: Daniel Cothran <daniel@andile.co>
Date: Sun, 4 Mar 2018 01:42:09 -0500
Subject: [PATCH] Issue #2936607 by andileco: [Plan] Further develop blocks
 integration

---
 charts.module                                 | 18 ++----
 includes/charts.pages.inc                     |  2 +
 .../charts_api_example.module                 |  2 +-
 .../src/Controller/ChartsApiExample.php       |  6 +-
 modules/charts_blocks/charts_blocks.module    | 13 -----
 .../src/Plugin/Block/ChartsBlock.php          | 57 +++++++++++++++----
 modules/charts_c3/src/Plugin/chart/C3.php     |  2 +-
 7 files changed, 58 insertions(+), 42 deletions(-)

diff --git a/charts.module b/charts.module
index 0a01040..170bd40 100644
--- a/charts.module
+++ b/charts.module
@@ -93,19 +93,9 @@ function template_preprocess_views_view_charts(&$variables) {
   $seriesData = array_merge($seriesData, $seriesDataAttachment);
 
   $plugin_manager = \Drupal::service('plugin.manager.charts');
-  $plugin_definitions = $plugin_manager->getDefinitions();
+  $plugin = $plugin_manager->createInstance($library);
+  $variables['height'] = $options['height'];
+  $variables['width'] = $options['width'];
+  $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId);
 
-  if (!isset($plugin_definitions)) {
-    // To be removed in drupal 9.
-    $moduleSelector = new ModuleSelector($library, $categories, $seriesData, $attachmentDisplayOptions, $options, $chartId);
-    if ($moduleSelector->moduleExists()) {
-      $moduleSelector->buildVariables($variables);
-    }
-  }
-  else {
-    $plugin = $plugin_manager->createInstance($library);
-    $variables['height'] = $options['height'];
-    $variables['width'] = $options['width'];
-    $plugin->buildVariables($options, $categories, $seriesData, $attachmentDisplayOptions, $variables, $chartId);
-  }
 }
diff --git a/includes/charts.pages.inc b/includes/charts.pages.inc
index 081890a..f59e2e5 100644
--- a/includes/charts.pages.inc
+++ b/includes/charts.pages.inc
@@ -686,6 +686,7 @@ function charts_default_settings() {
   $defaults = [
     'type'                  => 'line',
     'library'               => NULL,
+    'grouping'              => FALSE,
     'label_field'           => NULL,
     'data_fields'           => NULL,
     'field_colors'          => NULL,
@@ -697,6 +698,7 @@ function charts_default_settings() {
     'colors'                => charts_default_colors(),
     'background'            => '',
     'three_dimensional'     => FALSE,
+    'polar'                 => FALSE,
     'tooltips'              => TRUE,
     'tooltips_use_html'     => FALSE,
     'width'                 => NULL,
diff --git a/modules/charts_api_example/charts_api_example.module b/modules/charts_api_example/charts_api_example.module
index 1da3ee0..ee89730 100644
--- a/modules/charts_api_example/charts_api_example.module
+++ b/modules/charts_api_example/charts_api_example.module
@@ -47,5 +47,5 @@ function charts_api_example_theme() {
 function template_preprocess_charts_api_example(&$variables) {
   $plugin_manager = \Drupal::service('plugin.manager.charts');
   $plugin = $plugin_manager->createInstance($variables['library']);
-  $plugin->buildVariables($variables['options'], $variables['categories'], $variables['seriesData'], [], $variables, 'xyz');
+  $plugin->buildVariables($variables['options'], $variables['categories'], $variables['seriesData'], [], $variables, $variables['id']);
 }
diff --git a/modules/charts_api_example/src/Controller/ChartsApiExample.php b/modules/charts_api_example/src/Controller/ChartsApiExample.php
index fe7e872..d646b6f 100644
--- a/modules/charts_api_example/src/Controller/ChartsApiExample.php
+++ b/modules/charts_api_example/src/Controller/ChartsApiExample.php
@@ -71,18 +71,22 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter
           'type'  => 'area',
           'data'  => [0, 0, 60, 90],
         ];
-      // If using C3, comment out the following two cases.
       case 'pie':
       case 'donut':
 
     }
 
+    // Creates a UUID for the chart ID.
+    $uuid_service = \Drupal::service('uuid');
+    $chartId = 'chart-' . $uuid_service->generate();
+
     $build = [
       '#theme'      => 'charts_api_example',
       '#library'    => (string) $library,
       '#categories' => $categories,
       '#seriesData' => $seriesData,
       '#options'    => $options,
+      '#id'         => $chartId,
     ];
 
     return $build;
diff --git a/modules/charts_blocks/charts_blocks.module b/modules/charts_blocks/charts_blocks.module
index ac9f681..c7406a6 100644
--- a/modules/charts_blocks/charts_blocks.module
+++ b/modules/charts_blocks/charts_blocks.module
@@ -54,16 +54,3 @@ function template_preprocess_charts_blocks(&$variables) {
   $plugin = $plugin_manager->createInstance($variables['library']);
   $plugin->buildVariables($variables['options'], $variables['categories'], $variables['seriesData'], [], $variables, $variables['id']);
 }
-
-///**
-// * Implements template_preprocess_page
-// *
-// * @param $variables
-// */
-//function charts_blocks_preprocess_block(&$variables) {
-//  if($variables['plugin_id'] == 'charts_block') {
-//    $plugin_manager = \Drupal::service('plugin.manager.charts');
-//    $plugin = $plugin_manager->createInstance($variables['content']['#library']);
-//    $plugin->buildVariables($variables['content']['#options'], $variables['content']['#categories'], $variables['content']['#seriesData'], [], $variables['content']['#options'], 'id');
-//  }
-//}
\ No newline at end of file
diff --git a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
index d3876d2..b0df36f 100644
--- a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
+++ b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php
@@ -109,6 +109,14 @@ class ChartsBlock extends BlockBase {
       '#default_value' => $this->configuration['categories'],
       '#weight' => '-16',
     ];
+    // Enable stacking.
+    $form['grouping'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Stacking'),
+      '#description' => $this->t('Enable stacking'),
+      '#default_value' => $this->configuration['grouping'],
+      '#weight' => '-15',
+    ];
 
     /**
      * Unset the #parents element from default form, then set the
@@ -152,9 +160,32 @@ class ChartsBlock extends BlockBase {
     unset($form['display']['dimensions']['width']['#parents']);
     $form['display']['dimensions']['width']['#default_value'] = $this->configuration['width'];
 
-    /**
-     * @todo: complete this for the remaining form elements.
-     */
+    unset($form['xaxis']['xaxis_title']['#parents']);
+    $form['xaxis']['xaxis_title']['#default_value'] = $this->configuration['xaxis_title'];
+
+    unset($form['xaxis']['xaxis_labels_rotation']['#parents']);
+    $form['xaxis']['xaxis_labels_rotation']['#default_value'] = $this->configuration['xaxis_labels_rotation'];
+
+    unset($form['yaxis']['yaxis_title']['#parents']);
+    $form['yaxis']['yaxis_title']['#default_value'] = $this->configuration['yaxis_title'];
+
+    unset($form['yaxis']['yaxis_min']['#parents']);
+    $form['yaxis']['yaxis_min']['#default_value'] = $this->configuration['yaxis_min'];
+
+    unset($form['yaxis']['yaxis_max']['#parents']);
+    $form['yaxis']['yaxis_max']['#default_value'] = $this->configuration['yaxis_max'];
+
+    unset($form['yaxis']['yaxis_prefix']['#parents']);
+    $form['yaxis']['yaxis_prefix']['#default_value'] = $this->configuration['yaxis_prefix'];
+
+    unset($form['yaxis']['yaxis_suffix']['#parents']);
+    $form['yaxis']['yaxis_suffix']['#default_value'] = $this->configuration['yaxis_suffix'];
+
+    unset($form['yaxis']['yaxis_decimal_count']['#parents']);
+    $form['yaxis']['yaxis_decimal_count']['#default_value'] = $this->configuration['yaxis_decimal_count'];
+
+    unset($form['yaxis']['yaxis_labels_rotation']['#parents']);
+    $form['yaxis']['yaxis_labels_rotation']['#default_value'] = $this->configuration['yaxis_labels_rotation'];
 
     return $form;
   }
@@ -171,6 +202,7 @@ class ChartsBlock extends BlockBase {
     $this->configuration['data_series'] = $form_state->getValue('data_series');
     $this->configuration['series_label'] = $form_state->getValue('series_label');
     $this->configuration['categories'] = $form_state->getValue('categories');
+    $this->configuration['grouping'] = $form_state->getValue('grouping');
     $this->configuration['field_colors'] = $form_state->getValue('field_colors');
     $this->configuration['title'] = $form_state->getValue(['display','title']);
     $this->configuration['title_position'] = $form_state->getValue(['display','title_position']);
@@ -185,15 +217,15 @@ class ChartsBlock extends BlockBase {
     $this->configuration['tooltips_use_html'] = $form_state->getValue('tooltips_use_html');
     $this->configuration['width'] = $form_state->getValue(['display','dimensions','width']);
     $this->configuration['height'] = $form_state->getValue(['display','dimensions','height']);
-    $this->configuration['xaxis_title'] = $form_state->getValue('xaxis_title');
-    $this->configuration['xaxis_labels_rotation'] = $form_state->getValue('xaxis_labels_rotation');
-    $this->configuration['yaxis_title'] = $form_state->getValue('yaxis_title');
-    $this->configuration['yaxis_min'] = $form_state->getValue('yaxis_min');
-    $this->configuration['yaxis_max'] = $form_state->getValue('yaxis_max');
-    $this->configuration['yaxis_prefix'] = $form_state->getValue('yaxis_prefix');
-    $this->configuration['yaxis_suffix'] = $form_state->getValue('yaxis_suffix');
-    $this->configuration['yaxis_decimal_count'] = $form_state->getValue('yaxis_decimal_count');
-    $this->configuration['yaxis_labels_rotation'] = $form_state->getValue('yaxis_labels_rotation');
+    $this->configuration['xaxis_title'] = $form_state->getValue(['xaxis', 'xaxis_title']);
+    $this->configuration['xaxis_labels_rotation'] = $form_state->getValue(['xaxis', 'xaxis_labels_rotation']);
+    $this->configuration['yaxis_title'] = $form_state->getValue(['yaxis', 'yaxis_title']);
+    $this->configuration['yaxis_min'] = $form_state->getValue(['yaxis', 'yaxis_min']);
+    $this->configuration['yaxis_max'] = $form_state->getValue(['yaxis', 'yaxis_max']);
+    $this->configuration['yaxis_prefix'] = $form_state->getValue(['yaxis', 'yaxis_prefix']);
+    $this->configuration['yaxis_suffix'] = $form_state->getValue(['yaxis', 'yaxis_suffix']);
+    $this->configuration['yaxis_decimal_count'] = $form_state->getValue(['yaxis', 'yaxis_decimal_count']);
+    $this->configuration['yaxis_labels_rotation'] = $form_state->getValue(['yaxis', 'yaxis_labels_rotation']);
 
   }
 
@@ -205,6 +237,7 @@ class ChartsBlock extends BlockBase {
     $options = [
       'library' => $this->configuration['library'],
       'type' => $this->configuration['type'],
+      'grouping' => $this->configuration['grouping'],
       'field_colors'=>$this->configuration['field_colors'],
       'title'=>$this->configuration['title'],
       'title_position'=>$this->configuration['title_position'],
diff --git a/modules/charts_c3/src/Plugin/chart/C3.php b/modules/charts_c3/src/Plugin/chart/C3.php
index 8007823..d130c21 100644
--- a/modules/charts_c3/src/Plugin/chart/C3.php
+++ b/modules/charts_c3/src/Plugin/chart/C3.php
@@ -83,7 +83,7 @@ class C3 extends AbstractChart {
         array_push($c3Data, $c3DataTemp);
       }
     }
-    
+
     $c3Chart = new ChartType();
     $c3Chart->setType($options['type']);
     $c3ChartTitle = new ChartTitle();
-- 
GitLab