You are here

function openlayers_examples in Openlayers 7.3

Form handler.

8 string references to 'openlayers_examples'
IconSprites::getJS in modules/openlayers_library/src/Plugin/Component/IconSprites/IconSprites.php
!Attention! This function will remove any option that is named after a plugin type e.g.: layers, controls, styles, interactions, components .
openlayers_examples_block_view in modules/openlayers_examples/openlayers_examples.module
Implements hook_block_view().
openlayers_examples_ctools_plugin_api in modules/openlayers_examples/openlayers_examples.module
Implements hook_ctools_plugin_api().
openlayers_examples_default_openlayers_components in modules/openlayers_examples/includes/openlayers_examples.default_openlayers_components.inc
Implements hook_default_openlayers_components().
openlayers_examples_default_openlayers_sources in modules/openlayers_examples/includes/openlayers_examples.default_openlayers_sources.inc
Implements hook_default_openlayers_sources().

... See full list

File

modules/openlayers_examples/openlayers_examples.module, line 38
Openlayers Examples.

Code

function openlayers_examples($form, &$form_state, $map_arg = NULL) {
  $maps = array();
  $options = array();
  foreach (\Drupal\openlayers\Openlayers::loadAllExportable('Map') as $machine_name => $data) {
    if (!is_object($data) || property_exists($data, 'disabled') && ($data->disabled == 1 || $data->disabled == TRUE)) {
      continue;
    }
    $options[$machine_name] = $data->name;
  }
  if (isset($form_state['values']['openlayers_default_map'])) {
    $maps = $form_state['values']['openlayers_default_map'];
  }
  else {
    $map = $map_arg;
    if ($map == NULL || !isset($options[$map])) {
      $maps = array(
        array_rand($options, 1),
      );
    }
    elseif (isset($options[$map])) {
      $maps = array(
        $map,
      );
    }
    if ($map == 'all') {
      $maps = array_keys($options);
    }
  }
  $form['openlayers_default_map'] = array(
    '#type' => 'select',
    '#multiple' => TRUE,
    '#options' => $options,
    '#default_value' => $maps,
    '#ajax' => array(
      'callback' => '_openlayers_examples_ajax_reload_default_map',
      'method' => 'replace',
      'wrapper' => 'default_openlayers_map_ajax',
      'effect' => 'fade',
    ),
  );
  $form['#token'] = NULL;
  $form['form'] = array(
    '#prefix' => '<div id="default_openlayers_map_ajax">',
    '#suffix' => '</div>',
    '#type' => 'markup',
  );
  foreach ($maps as $map) {
    if (($map = \Drupal\openlayers\Openlayers::load('Map', $map)) == FALSE) {
      continue;
    }
    $form['form'][$map
      ->getId()] = array(
      '#type' => 'fieldset',
      '#title' => $map
        ->getName(),
      '#collapsible' => FALSE,
    );
    $map
      ->clearOption('contextualLinks');
    $controls = array_values(array_merge($map
      ->getOption('controls', array()), array(
      'openlayers_control_attribution',
      'openlayers_control_zoom',
      'openlayers_control_rotate',
      'openlayers_control_fullscreen',
    )));
    $interactions = array_values(array_merge($map
      ->getOption('interactions', array()), array(
      'openlayers_interaction_doubleclickzoom',
      'openlayers_interaction_dragpan',
      'openlayers_interaction_dragrotate',
    )));
    $components = array_values(array_merge($map
      ->getOption('components', array()), array(
      'openlayers_examples_component_ajax_values',
      'openlayers_examples_component_viewsync',
      'openlayers_examples_component_spinjs',
      'openlayers_examples_component_progressbar',
      'openlayers_examples_component_permalink',
    )));
    $map
      ->setOption('height', '500px')
      ->setOption('controls', $controls)
      ->setOption('interactions', $interactions)
      ->setOption('components', $components);
    if (isset($form_state['input']['zoom'])) {
      $map
        ->setOption(array(
        'view',
        'zoom',
      ), $form_state['input']['zoom']);
    }
    if (isset($form_state['input']['rot'])) {
      $map
        ->setOption(array(
        'view',
        'rotation',
      ), $form_state['input']['rot']);
    }
    if (isset($form_state['input']['lat'])) {
      $map
        ->setOption(array(
        'view',
        'center',
        'lat',
      ), $form_state['input']['lat']);
    }
    if (isset($form_state['input']['lon'])) {
      $map
        ->setOption(array(
        'view',
        'center',
        'lon',
      ), $form_state['input']['lon']);
    }
    $form['form'][$map
      ->getId()]['map'] = array(
      '#weight' => 0,
      '#type' => 'openlayers',
      '#map' => $map,
    );
    if (module_exists('openlayers_services')) {
      if ((bool) $map
        ->getOption('provideIframe', FALSE) == TRUE) {
        $form_state['item'] = $map;
        $form['form'][$map
          ->getId()]['openlayers_services'] = array(
          '#type' => 'fieldset',
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
          '#title' => 'Embed this map in an iframe',
        );
        foreach (services_endpoint_load_all() as $service) {
          if (isset($service->resources['openlayers-map']['operations']['retrieve']['enabled']) && $service->resources['openlayers-map']['operations']['retrieve']['enabled'] == 1) {
            $width = $map
              ->getOption('width', 'auto') == 'auto' ? '100%' : $map
              ->getOption('width', 'auto');
            $height = $map
              ->getOption('height', 'auto');
            $iframe = "<iframe src='" . url('openlayers/openlayers-map/' . $map
              ->getMachineName(), array(
              'absolute' => TRUE,
            )) . "' width='" . $width . "' height='" . $height . "'/>";
            $form['form'][$map
              ->getId()]['openlayers_services'][$map
              ->getMachineName()] = array(
              '#type' => 'textarea',
              '#description' => t('Copy paste this html code to embed this map in your content.'),
              '#rows' => 3,
              '#value' => $iframe,
            );
          }
        }
      }
    }
    $form['form'][$map
      ->getId()]['permalink'] = array(
      '#weight' => 6,
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => 'Permanent link to this map',
    );
    $form['form'][$map
      ->getId()]['permalink']['link'] = array(
      '#type' => 'textfield',
      '#value' => url('openlayers_examples/' . $map
        ->getMachineName(), array(
        'absolute' => TRUE,
      )),
      '#size' => 128,
    );
  }
  $form['lat'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'id' => 'lat',
    ),
    '#value' => 0,
  );
  $form['lon'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'id' => 'lon',
    ),
    '#value' => 0,
  );
  $form['rot'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'id' => 'rot',
    ),
    '#value' => 0,
  );
  $form['zoom'] = array(
    '#type' => 'hidden',
    '#attributes' => array(
      'id' => 'zoom',
    ),
    '#value' => 0,
  );
  $form['version'] = array(
    '#markup' => 'Module running Openlayers version: ' . \Drupal\openlayers\Openlayers::getLibraryVersion(),
  );

  // This is for preventing 'system/ajax' as destination when using AJAX maps.
  // See: http://drupal.stackexchange.com/questions/86958/get-current-url-arguments-inside-ajax-callback
  $form['current_path'] = array(
    '#type' => 'hidden',
    '#value' => current_path(),
  );

  // Make a system setting form and return.
  return $form;
}