You are here

protected function WebformLocationGeocomplete::formatHtmlItem in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_location_geocomplete/src/Plugin/WebformElement/WebformLocationGeocomplete.php \Drupal\webform_location_geocomplete\Plugin\WebformElement\WebformLocationGeocomplete::formatHtmlItem()

Format an element's value as HTML.

Parameters

array $element: An element.

\Drupal\webform\WebformSubmissionInterface $webform_submission: A webform submission.

array $options: An array of options.

Return value

array|string The element's value formatted as HTML or a render array.

Overrides WebformCompositeBase::formatHtmlItem

File

modules/webform_location_geocomplete/src/Plugin/WebformElement/WebformLocationGeocomplete.php, line 53

Class

WebformLocationGeocomplete
Provides an 'location' element using Geocomplete.

Namespace

Drupal\webform_location_geocomplete\Plugin\WebformElement

Code

protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
  $value = $this
    ->getValue($element, $webform_submission, $options);

  // Return empty value.
  if (empty($value) || empty(array_filter($value))) {
    return '';
  }
  $format = $this
    ->getItemFormat($element);
  if ($format === 'map') {
    $google_map_url = UrlGenerator::fromUri('http://maps.google.com/', [
      'query' => [
        'q' => $value['value'],
      ],
    ]);
    $location = $value['location'];
    $key = isset($element['#api_key']) ? $element['#api_key'] : $this->configFactory
      ->get('webform.settings')
      ->get('element.default_google_maps_api_key');
    $center = urlencode($value['location']);
    $image_map_uri = "https://maps.googleapis.com/maps/api/staticmap?zoom=14&size=600x338&markers=color:red%7C{$location}&key={$key}&center={$center}";
    return [
      'location' => [
        '#type' => 'link',
        '#title' => $value['value'],
        '#url' => $google_map_url,
        '#suffix' => '<br />',
      ],
      'map' => [
        '#type' => 'link',
        '#title' => [
          '#theme' => 'image',
          '#uri' => $image_map_uri,
          '#width' => 600,
          '#height' => 338,
          '#alt' => $value['value'],
          '#attributes' => [
            'style' => "display: block; max-width: 100%; height: auto; border: 1px solid #ccc;",
          ],
        ],
        '#url' => $google_map_url,
        '#suffix' => '<br />',
      ],
    ];
  }
  else {
    return parent::formatHtmlItem($element, $webform_submission, $options);
  }
}