You are here

kml.module in KML 8

Same filename and directory in other branches
  1. 5 kml.module
  2. 6.2 kml.module
  3. 6 kml.module
  4. 7 kml.module

Adds support for serializing entities to KML.

File

kml.module
View source
<?php

/**
 * @file
 * Adds support for serializing entities to KML.
 */
use Drupal\Core\Routing\RouteMatchInterface;

/**
 * Implements hook_help().
 */
function kml_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.kml':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('This module allows for export of location-specific data as placemarks in KML and KMZ formats.') . '</p>';
      return $output;
  }
}
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function kml_form_views_ui_edit_display_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['options']['style_options']['formats']['#options']) && in_array('kml', $form['options']['style_options']['formats']['#options'])) {

    /** @var \Drupal\views\Entity\View $view */
    $view = $form_state
      ->getStorage()['view'];
    $display = $view
      ->getExecutable()
      ->getDisplay();

    // KML options.
    $form['options']['style_options']['kml_settings'] = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => t('KML settings'),
      '#states' => [
        'visible' => [
          [
            ':input[name="style_options[formats][kml]"]' => [
              'checked' => TRUE,
            ],
          ],
          'or',
          [
            ':input[name="style_options[formats][kmz]"]' => [
              'checked' => TRUE,
            ],
          ],
        ],
      ],
      'kml_header' => [
        '#type' => 'textarea',
        '#title' => t('KML Document header'),
        '#description' => t('What goes here will be inserted between the &lt;Document&gt; tag and the first &lt;Placemark&gt; tag. Must be valid KML. Can include &lt;name&gt;, &lt;description&gt;, &lt;atom:author&gt;, style table etc.'),
        '#default_value' => $display->display['display_options']['style']['options']['kml_settings']['kml_header'],
      ],
    ];
  }
  if (isset($form['options']['row_options']['field_options'])) {

    /** @var \Drupal\views\Entity\View $view */
    $view = $form_state
      ->getStorage()['view'];
    $display = $view
      ->getExecutable()
      ->getDisplay();
    if (isset($display->display['display_options']['style']['options']['formats']['kml']) || isset($display->display['display_options']['style']['options']['formats']['kmz'])) {
      foreach ($form['options']['row_options']['field_options']['#header'] as &$header) {
        if ($header
          ->getUntranslatedString() === 'Alias') {
          $header = t('KML Tag');
        }
      }
      foreach ($form['options']['row_options']['field_options'] as $key => &$field_option) {
        if (strpos($key, '#') !== 0) {
          $field_option['alias']['#type'] = 'select';
          $field_option['alias']['#options'] = [
            'name' => 'name',
            'description' => 'description',
            'Point_coordinates' => 'Point_coordinates',
            'LineString_coordinates' => 'LineString_coordinates',
            'icon' => 'icon',
            'styleUrl' => 'styleUrl',
            'atom_author' => 'atom_author',
            'atom_link' => 'atom_link',
            'gx_media_links' => 'gx_media_links',
            'Folder' => 'Folder',
          ];
          $field_option['alias']['#empty_option'] = t('Preformatted KML');
        }
      }
    }
  }
}