From 8a0d645ae39e3bbcce04e3379a475dbb7f2e2e05 Mon Sep 17 00:00:00 2001 From: Daniel Cothran <daniel@andile.co> Date: Sat, 24 Feb 2018 03:14:30 -0500 Subject: [PATCH] Make Charts API Example module use plugins, since the render files were deleted. Now more closely resembles Charts Blocks. --- includes/charts.pages.inc | 2 -- modules/charts_api_example/charts_api_example.module | 8 ++++---- .../src/Controller/ChartsApiExample.php | 2 ++ .../templates/charts_api_example.html.twig | 7 ++----- modules/charts_blocks/src/Plugin/Block/ChartsBlock.php | 10 +++------- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/includes/charts.pages.inc b/includes/charts.pages.inc index 160f35c..081890a 100644 --- a/includes/charts.pages.inc +++ b/includes/charts.pages.inc @@ -286,8 +286,6 @@ function charts_settings_form($form, $defaults = [], $field_options = [], $paren if ($field_options) { $first_field = key($field_options); - /* $form['#theme'] = 'charts_settings_fields'; */ - $form['fields'] = [ '#title' => t('Charts fields'), '#type' => 'fieldset', diff --git a/modules/charts_api_example/charts_api_example.module b/modules/charts_api_example/charts_api_example.module index 1c5d099..1da3ee0 100644 --- a/modules/charts_api_example/charts_api_example.module +++ b/modules/charts_api_example/charts_api_example.module @@ -35,6 +35,7 @@ function charts_api_example_theme() { 'categories' => [], 'seriesData' => [], 'options' => [], + 'id' => '', ], ], ]; @@ -44,8 +45,7 @@ function charts_api_example_theme() { * Implements template_preprocess_charts_api_example(). */ function template_preprocess_charts_api_example(&$variables) { - $moduleSelector = new ModuleSelector($variables['library'], $variables['categories'], $variables['seriesData'], $variables['options'], [], 'xyz'); - if ($moduleSelector->moduleExists()) { - $moduleSelector->buildVariables($variables); - } + $plugin_manager = \Drupal::service('plugin.manager.charts'); + $plugin = $plugin_manager->createInstance($variables['library']); + $plugin->buildVariables($variables['options'], $variables['categories'], $variables['seriesData'], [], $variables, 'xyz'); } diff --git a/modules/charts_api_example/src/Controller/ChartsApiExample.php b/modules/charts_api_example/src/Controller/ChartsApiExample.php index e0d54dc..fe7e872 100644 --- a/modules/charts_api_example/src/Controller/ChartsApiExample.php +++ b/modules/charts_api_example/src/Controller/ChartsApiExample.php @@ -37,6 +37,8 @@ class ChartsApiExample extends ControllerBase implements ContainerInjectionInter drupal_set_message($this->t('You need to first configure Charts default settings')); return []; } + + // Customize options here. $options = [ 'type' => $this->chartSettings['type'], 'title' => $this->t('Chart title'), diff --git a/modules/charts_api_example/templates/charts_api_example.html.twig b/modules/charts_api_example/templates/charts_api_example.html.twig index 561c1e1..734319d 100644 --- a/modules/charts_api_example/templates/charts_api_example.html.twig +++ b/modules/charts_api_example/templates/charts_api_example.html.twig @@ -1,6 +1,3 @@ -{% set library, height, width = 'charts_' ~ chart_type ~ '/' ~ chart_type, options.height, options.width %} +{% set library, height, width = 'charts_' ~ library ~ '/' ~ library, options.height, options.width %} {{ attach_library("#{ library }") }} -<p>Edit charts_api_example.html.twig to delete this message. To change the - library used by this file, change the <a href="{{ url('charts.settings',{}) }}">default settings</a>. -</p> -<div {{ attributes }} {{ content_attributes }} style="width:{{ width }};height:{{ height }};"></div> +<div {{ attributes }} {{ content_attributes }} style="{% if width is not empty %}width:{{ width }}px;{% endif %}{% if height is not empty %}height:{{ height }}px;{% endif %}"></div> diff --git a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php index fdd9f33..d3876d2 100644 --- a/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php +++ b/modules/charts_blocks/src/Plugin/Block/ChartsBlock.php @@ -26,7 +26,6 @@ class ChartsBlock extends BlockBase { // Get the default chart values. $defaults = \Drupal::state()->get('charts_default_settings', []); - $defaults += charts_default_settings(); foreach ($defaults as $default_key => $default_value) { $options[$default_key]['default'] = $default_value; @@ -247,12 +246,9 @@ class ChartsBlock extends BlockBase { ]; } - /** - * @todo: refactor the way this id is produced to ensure uniqueness. - */ - $chartId = strtolower($this->label()); - $chartId = preg_replace('/[^a-z0-9_]+/', '_', $chartId); - $chartId = preg_replace('/_+/', '_', $chartId); + // Creates a UUID for the chart ID. + $uuid_service = \Drupal::service('uuid'); + $chartId = 'chart-' . $uuid_service->generate(); $build = [ '#theme' => 'charts_blocks', -- GitLab