-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPreviewHelper.php
More file actions
81 lines (67 loc) · 2.53 KB
/
PreviewHelper.php
File metadata and controls
81 lines (67 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
namespace Drupal\quanthub_visualization\Service;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Visualization preview helper service.
*/
class PreviewHelper {
/**
* Constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(protected EntityTypeManagerInterface $entityTypeManager) {
}
/**
* Collect all necessary data for previews.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Form state.
*
* @return array
* Prepared form.
*/
public function provideDrupalSettingsData(FormStateInterface $form_state) {
$nodeStorage = $this->entityTypeManager->getStorage('node');
$datasetUrn = $nodeStorage->load($form_state->getValue('field_dataset')[0]['target_id'])->get('field_quanthub_urn')->getString();
$filters = $form_state->getValue('field_qh_visualization_filters')[0]['value'];
unset($filters['add_more']);
foreach ($filters as &$filter) {
unset($filter['_weight']);
}
$filters = array_filter($filters, function ($item) {
if (empty($item['componentCode']) && empty($item['operator']) && empty($item['value'])) {
return FALSE;
}
else {
return TRUE;
}
});
$displayConfig = $form_state->getValue('field_qh_visualization_transform')[0]['value'];
$dataTransformationsConfig['display'] = [];
// Build dataTransformationsConfig.
foreach ($displayConfig as $item) {
if (is_array($item) && !empty($item['dimensionId'])) {
$dataTransformationsConfig['display'][] = [
'dimensionId' => $item['dimensionId'],
'field' => $item['field'],
];
}
}
$visualizationConfig = $form_state->getValue('field_media_qh_visualization')[0]['value'];
$dafnaType = $form_state->getValue('field_qh_visualization_type')[0]['value'];
$dataFilters = $form_state->getValue('field_visualization_filters')[0]['value'];
$preview_data['#attached']['drupalSettings']['quanthubVisualization']['media'][0] = [
'dafnaType' => $dafnaType,
'dataFilters' => empty($dataFilters) ? NULL : $dataFilters,
'visualizationTitle' => $form_state->getValue('name')[0]['value'],
'visualizationConfig' => json_decode($visualizationConfig),
'dataTransformationsConfig' => $dataTransformationsConfig,
'dataSourceConfig' => ['timeFilters' => $filters],
'dataSource' => $datasetUrn,
];
return $preview_data;
}
}