|
5 | 5 | use Drupal\Core\Form\FormStateInterface; |
6 | 6 | use Drupal\leaflet\LeafletSettingsElementsTrait; |
7 | 7 | use Drupal\webform\Plugin\WebformElementBase; |
| 8 | +use Drupal\webform\WebformSubmissionInterface; |
8 | 9 |
|
9 | 10 | /** |
10 | 11 | * Provides a 'webform_map_field' element. |
@@ -70,6 +71,10 @@ public function defineDefaultProperties(): array { |
70 | 71 | 'circle_color' => '#3388FF', |
71 | 72 | 'rectangle_color' => '#3388FF', |
72 | 73 |
|
| 74 | + // Display settings. |
| 75 | + 'display_image_on' => ['html', 'pdf'], |
| 76 | + 'display_geojson_on' => ['text', 'html'], |
| 77 | + |
73 | 78 | ] + parent::defineDefaultProperties(); |
74 | 79 | } |
75 | 80 |
|
@@ -344,7 +349,111 @@ public function form(array $form, FormStateInterface $form_state) { |
344 | 349 | ], |
345 | 350 | ]; |
346 | 351 |
|
| 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 | + |
347 | 377 | return $form; |
348 | 378 | } |
349 | 379 |
|
| 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 | + |
350 | 459 | } |
0 commit comments