You are here

function kml_form_views_ui_edit_display_form_alter in KML 8

Implements hook_form_BASE_FORM_ID_alter().

File

./kml.module, line 28
Adds support for serializing entities to KML.

Code

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 <Document> tag and the first <Placemark> tag. Must be valid KML. Can include <name>, <description>, <atom:author>, 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');
        }
      }
    }
  }
}