Skip to content
Snippets Groups Projects
Commit dd8dd57a authored by Daniel Cothran's avatar Daniel Cothran Committed by Daniel Cothran
Browse files

Issue #3387130 by andileco, nikathone: Adjust processNumberValueFromField() to...

Issue #3387130 by andileco, nikathone: Adjust processNumberValueFromField() to be more generic in detecting charts-specific Views fields
parent 24ab8e09
No related branches found
No related tags found
No related merge requests found
Pipeline #19719 passed with warnings
<?php
namespace Drupal\charts;
interface ChartViewsFieldInterface {
/**
* Get the chart field data type.
*
* @return string
*/
public function getChartFieldDataType(): string;
}
......@@ -2,6 +2,7 @@
namespace Drupal\charts\Plugin\views\field;
use Drupal\charts\ChartViewsFieldInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
......@@ -21,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @ingroup views_field_handlers
* @ViewsField("field_charts_fields_bubble")
*/
class BubbleField extends FieldPluginBase implements ContainerFactoryPluginInterface {
class BubbleField extends FieldPluginBase implements ContainerFactoryPluginInterface, ChartViewsFieldInterface {
/**
* The messenger service.
......@@ -204,4 +205,14 @@ class BubbleField extends FieldPluginBase implements ContainerFactoryPluginInter
]);
}
/**
* Set the data type for the chart field to be an array.
*
* @return string
* The data type.
*/
public function getChartFieldDataType(): string {
return 'array';
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\charts\Plugin\views\field;
use Drupal\charts\ChartViewsFieldInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
......@@ -21,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
* @ingroup views_field_handlers
* @ViewsField("field_charts_fields_scatter")
*/
class ScatterField extends FieldPluginBase implements ContainerFactoryPluginInterface {
class ScatterField extends FieldPluginBase implements ContainerFactoryPluginInterface, ChartViewsFieldInterface {
/**
* The messenger service.
......@@ -181,12 +182,20 @@ class ScatterField extends FieldPluginBase implements ContainerFactoryPluginInte
$xAxisFieldValue = $this->getFieldValue($values, TRUE);
$yAxisFieldValue = $this->getFieldValue($values, FALSE);
$value = Json::encode([
return Json::encode([
Json::decode($xAxisFieldValue),
Json::decode($yAxisFieldValue),
]);
}
return $value;
/**
* Set the data type for the chart field to be an array.
*
* @return string
* The data type.
*/
public function getChartFieldDataType(): string {
return 'array';
}
}
......@@ -2,8 +2,9 @@
namespace Drupal\charts\Plugin\views\style;
use Drupal\charts\Plugin\chart\Library\ChartInterface;
use Drupal\charts\ChartManager;
use Drupal\charts\ChartViewsFieldInterface;
use Drupal\charts\Plugin\chart\Library\ChartInterface;
use Drupal\charts\TypeManager;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\Html;
......@@ -605,10 +606,13 @@ class ChartsPluginStyleChart extends StylePluginBase implements ContainerFactory
$value = trim(strip_tags($value));
if (strpos($field, 'field_charts_fields_scatter') === 0 || strpos($field, 'field_charts_fields_bubble') === 0) {
// Get the field plugin class to determine if a Charts-specific field
// is being used.
$field_plugin = $this->view->field[$field];
if ($field_plugin instanceof ChartViewsFieldInterface && $field_plugin->getChartFieldDataType() === 'array') {
return Json::decode($value);
}
// Convert empty strings to NULL.
if ($value === '' || is_null($value)) {
$value = NULL;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment