openlayers-8.x-4.x-dev/src/Form/SettingsForm.php

src/Form/SettingsForm.php
<?php

namespace Drupal\openlayers\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Contains the Admin Form for the Openlayers module.
 */
class SettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'openlayers.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'openlayers_settings_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Form constructor.
    $form = parent::buildForm($form, $form_state);
    
    $config = $this->config('openlayers.settings');

    $js_css_group = 'openlayers';

    $libraryPath = DRUPAL_ROOT . '/libraries/openlayers';
    if (file_exists($libraryPath) && is_dir($libraryPath)) {
      $versions = scandir($libraryPath . '/versions');
      $local_variants = [];
      foreach ($versions as $version) {
        $variant_path = $libraryPath . '/versions/' . $version;
        if (is_dir($variant_path) && substr($version, 0, 1) == 'v') {
          $jsdir = '/build';
          $cssdir = '/css';
          // Might be a reduced fingerprint distribution.
          if (!is_dir($variant_path . '/build')) {
            $jsdir = '';
            $cssdir = '';
          }
          $local_variants['local-' . substr($version, 1)] = [
            'name' => 'Openlayers ' . $version,
            'files' => array(
              'js' => array(
                'versions/' . $version . $jsdir . '/ol.js' => array(
                  'type' => 'file',
                  'weight' => 6,
                  'group' => $js_css_group,
                ),
              ),
              'css' => array(
                'versions/' . $version . $cssdir . '/ol.css' => array(
                  'type' => 'file',
                  'weight' => 4,
                  'group' => $js_css_group,
                ),
              ),
            ),
          ];
        }
      }

      //  TODO - it should be possible to derive the cdn versions from the libraries.yml file.
      $cdn_variants = array(
        'cdn-7.0.0' => array(
          'name' => 'Openlayers v7.0.0 (CDN)'
        ),
        
        'cdn-7.1.0' => array(
          'name' => 'Openlayers v7.1.0 (CDN)'
        ),

        'cdn-7.2.2' => array(
          'name' => 'Openlayers v7.2.2 (CDN)'
        ),

        'cdn-7.3.0' => array(
          'name' => 'Openlayers v7.3.0 (CDN)'
        ),

        'cdn-8.2.0' => array(
          'name' => 'Openlayers v8.2.0 (CDN)'
        ),
      );

    }

    $form['origin'] = array(
      '#type' => 'fieldset',
      '#title' => 'Library loading configuration',
    );
    
    $form['#attached']['library'][] = 'openlayers/dummy';

    $current_variant = $config->get('variant');
    $options_variants = array('' => t('- Select the library variant -'));

    foreach ($local_variants as $version => $variant) {
      list($optgroup) = explode('-', $version, 2);
      if (empty($optgroup)) {
        $optgroup = t('Other');
      }
      $optgroup = mb_strtoupper($optgroup);
      $options_variants[$optgroup][$version] = (isset($variant['name'])) ? $variant['name'] : $version;
    }

    foreach ($cdn_variants as $version => $variant) {
      list($optgroup) = explode('-', $version, 2);
      if (empty($optgroup)) {
        $optgroup = t('Other');
      }
      $optgroup = mb_strtoupper($optgroup);
      $options_variants[$optgroup][$version] = (isset($variant['name'])) ? $variant['name'] : $version;
    }

    $form['origin']['variant'] = array(
      '#type' => 'select',
      '#title' => 'Select the Openlayers library variant to use:',
      '#options' => $options_variants,
      '#default_value' => $current_variant,
    );

    $form['origin']['debug'] = [
      '#type' => 'checkbox',
      '#title' => 'Load javascript debug integration files ?',
      '#default_value' => $config->get('debug'),
    ];

    $form['actions']['reset'] = [
      '#type' => 'submit',
      '#value' => $this->t('Reset to defaults'),
      '#name' => 'reset',
      '#button_type' => 'primary',
      '#weight' => 10,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $config = $this->config('openlayers.settings');
    $config->set('variant', $form_state->getValue('variant'));
    $config->set('debug', $form_state->getValue('debug'));
    $config->save();
    return parent::submitForm($form, $form_state);    
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc