Skip to content

Commit de9a7a1

Browse files
committed
Added “Display on“ options to Map element
1 parent b79b2b1 commit de9a7a1

4 files changed

Lines changed: 115 additions & 62 deletions

File tree

modules/os2forms_webform_maps/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ the map. The data can be exported to PDF.
1414
## Installation
1515

1616
The module can be installed using the standard Drupal installation procedure.
17+
18+
## Fetching GeoJSON using the API
19+
20+
``` shell
21+
@todo
22+
```

modules/os2forms_webform_maps/os2forms_webform_maps.module

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,6 @@
55
* Module file for os2forms_webform_maps.
66
*/
77

8-
/**
9-
* Implements hook_theme().
10-
*/
11-
function os2forms_webform_maps_theme() {
12-
return [
13-
'webform_element_base_html__webform_map_field' => [
14-
'variables' => [
15-
'element' => [],
16-
'value' => NULL,
17-
'webform_submission' => NULL,
18-
'options' => [],
19-
],
20-
],
21-
];
22-
}
23-
248
/**
259
* Implements hook_locale_translation_projects_alter().
2610
*/
@@ -29,31 +13,3 @@ function os2forms_webform_maps_locale_translation_projects_alter(&$projects) {
2913
$path = $module_handler->getModule('os2forms_webform_maps')->getPath();
3014
$projects['os2forms_webform_maps']['info']['interface translation server pattern'] = $path . '/translations/%language.po';
3115
}
32-
33-
/**
34-
* Implements hook_preprocess_webform_element_base_html__webform_map_field().
35-
*/
36-
function os2forms_webform_maps_preprocess_webform_element_base_html__webform_map_field(array &$variables) {
37-
// Decode the plain text value once.
38-
$decoded_value = json_decode($variables['value']['#plain_text']);
39-
40-
// Use the decoded geojson property.
41-
$variables['value']['#plain_text'] = $decoded_value->geojson;
42-
43-
// Load the webform element base HTML template.
44-
\Drupal::moduleHandler()->loadInclude('webform', 'inc', 'includes/webform.theme.template');
45-
template_preprocess_webform_element_base_html($variables);
46-
47-
// Generate a unique ID for the map image.
48-
$map_image_id = 'map-image-' . $variables['element']['#webform_key'];
49-
50-
$variables['map_image'] = [
51-
'#type' => 'html_tag',
52-
'#tag' => 'img',
53-
'#attributes' => [
54-
'class' => ['handler-help-message'],
55-
'id' => [$map_image_id],
56-
'src' => $decoded_value->image ?? '',
57-
],
58-
];
59-
}

modules/os2forms_webform_maps/src/Plugin/WebformElement/WebformLeafletMapField.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Form\FormStateInterface;
66
use Drupal\leaflet\LeafletSettingsElementsTrait;
77
use Drupal\webform\Plugin\WebformElementBase;
8+
use Drupal\webform\WebformSubmissionInterface;
89

910
/**
1011
* Provides a 'webform_map_field' element.
@@ -70,6 +71,10 @@ public function defineDefaultProperties(): array {
7071
'circle_color' => '#3388FF',
7172
'rectangle_color' => '#3388FF',
7273

74+
// Display settings.
75+
'display_image_on' => ['html', 'pdf'],
76+
'display_geojson_on' => ['text', 'html'],
77+
7378
] + parent::defineDefaultProperties();
7479
}
7580

@@ -344,7 +349,111 @@ public function form(array $form, FormStateInterface $form_state) {
344349
],
345350
];
346351

352+
$form['display_settings'] = [
353+
'#type' => 'fieldset',
354+
'#title' => $this->t('Display settings'),
355+
];
356+
$form['display_settings']['display_settings_container'] = [
357+
'display_image_on' => [
358+
'#type' => 'checkboxes',
359+
'#title' => $this->t('Display image on'),
360+
'#options' => [
361+
'html' => $this->t('HTML'),
362+
'pdf' => $this->t('PDF'),
363+
],
364+
],
365+
366+
'display_geojson_on' => [
367+
'#type' => 'checkboxes',
368+
'#title' => $this->t('Display GeoJSON on'),
369+
'#options' => [
370+
'html' => $this->t('HTML'),
371+
'pdf' => $this->t('PDF'),
372+
'text' => $this->t('Text'),
373+
],
374+
],
375+
];
376+
347377
return $form;
348378
}
349379

380+
/**
381+
* {@inheritdoc}
382+
*/
383+
protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []): array {
384+
$value = $this->getMapValue($element, $webform_submission, $options);
385+
386+
$imageId = 'map-image-' . $this->getKey($element);
387+
388+
$build = [];
389+
390+
$viewMode = $options['view_mode'] ?? 'overview';
391+
if ('table' === $viewMode) {
392+
$viewMode = 'html';
393+
}
394+
if ('html' === $viewMode && $options['pdf']) {
395+
$viewMode = 'pdf';
396+
}
397+
398+
// @todo Is this (i.e. $element['#display_image_on']) really the way to get element configuration?
399+
$showImageOn = array_filter((array) ($element['#display_image_on'] ?? NULL));
400+
$includeImage = isset($showImageOn[$viewMode]);
401+
402+
$showGeoJsonOn = array_filter((array) ($element['#display_geojson_on'] ?? NULL));
403+
$includeGeoJson = isset($showGeoJsonOn[$viewMode]);
404+
405+
if ($includeImage) {
406+
$build['image'] = [
407+
'#type' => 'html_tag',
408+
'#tag' => 'img',
409+
'#attributes' => [
410+
'class' => ['handler-help-message'],
411+
'id' => $imageId,
412+
'src' => $value['image'],
413+
],
414+
];
415+
}
416+
417+
if ($includeGeoJson) {
418+
$build['geojson'] = [
419+
'#markup' => $value['geojson'],
420+
];
421+
}
422+
423+
return $build;
424+
}
425+
426+
/**
427+
* {@inheritdoc}
428+
*/
429+
protected function formatTextItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
430+
$value = $this->getMapValue($element, $webform_submission, $options);
431+
432+
return $value['geojson'];
433+
}
434+
435+
/**
436+
* Get structured map value.
437+
*
438+
* @return array{
439+
* image: string,
440+
* geojson: string
441+
* }
442+
*/
443+
private function getMapValue(array $element, WebformSubmissionInterface $webform_submission, array $options = []): array {
444+
$value = $this->getValue($element, $webform_submission, $options);
445+
446+
try {
447+
$data = json_decode($value, associative: TRUE, flags: JSON_THROW_ON_ERROR);
448+
}
449+
catch (\JsonException) {
450+
$data = [];
451+
}
452+
453+
return $data + [
454+
'image' => '',
455+
'geojson' => 'null',
456+
];
457+
}
458+
350459
}

modules/os2forms_webform_maps/templates/webform-element-base-html--webform-map-field.html.twig

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)