openlayers-8.x-4.x-dev/modules/openlayers_geofield/src/Plugin/Field/FieldWidget/OpenlayersGeofieldWidget.php

modules/openlayers_geofield/src/Plugin/Field/FieldWidget/OpenlayersGeofieldWidget.php
<?php

namespace Drupal\openlayers_geofield\Plugin\Field\FieldWidget;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\openlayers\Plugin\Field\FieldWidget\OpenlayersWidgetBase;
use Drupal\openlayers\Openlayers;
use Drupal\openlayers\Map;
use Drupal\openlayers\Plugin\Source\Vector\Vector;

/**
 * Plugin implementation of an Openlayers widget.
 *
 * @FieldWidget(
 *   id = "openlayers_geofield_widget",
 *   label = @Translation("Openlayers Map"),
 *   description = @Translation("Provides an Openlayers field widget."),
 *   field_types = {
 *     "geofield",
 *   },
 * )
 */
class OpenlayersGeofieldWidget extends OpenlayersWidgetBase {

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'toolbar' => [
        'position' => 'top',
        'drawPoint' => TRUE,
        'drawPolyline' => TRUE,
        'drawPolygon' => TRUE,
        'cutHole' => TRUE,
        'editMode' => TRUE,
        'deleteMode' => TRUE,
      ],
      'show' => 0,
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $settings = $this->getSettings();

    $form = parent::settingsForm($form, $form_state);

    $form['input'] = [
      '#type' => 'fieldset',
      '#title' => $this->t('Geofield Settings'),
    ];
    $form['input']['show'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Show geofield input element'),
      '#default_value' => $settings['input']['show'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = [];
    $map_label = \Drupal::config('openlayers.map.' . $this->getSetting('map'))->get('label');
    $summary[] = $this->t('Openlayers map: @map', ['@map' => $map_label]);
    $summary[] = $this->t('Map height: @height @height_unit', ['@height' => $this->getSetting('height'), '@height_unit' => $this->getSetting('height_unit')]);
   
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
    $value = isset($items[$delta]->value) ? $items[$delta]->value : '';
    $settings = $this->getSettings();
    $map_name = $settings['map'];
    $map = Openlayers::load('Map', $map_name);
    $map->build();
    $entity = $items->getEntity();
    $entity_type = $entity->getEntityTypeId();
    $entity_id = $entity->id();
    $features = [];

    if ($value !== NULL && $value != '') {
      /** @var \Geometry $geom */
      $geom = \Drupal::service('geofield.geophp')->load($value, 'wkt');

      if ($geom->getGeomType() == 'GeometryCollection') {
        //  Set features.
        foreach ($geom->components as $component) {        
          $features[] = array(
            'wkt' => $component->out('wkt'),
            'projection' => 'EPSG:4326',
          );
        }
      } else {
          $features[] = array(
            'wkt' => $geom->out('wkt'),
            'projection' => 'EPSG:4326',
          );        
      }
    }
     

    $layer_name = 'ol_geofield';
    if ($layer = $map->getCollection()->getObjectById(['layer'], $layer_name)) {
      if ($layer->getSource() instanceof Vector) {
        $layer->getSource()->setOption('features', $features);
      }
    }

    // Render each map element as markup.

    // Add a specific map id.
    $map_id = 'openlayers-map' . '-' . $delta;
    
    //  Set height of map
    $height = $settings['height'] . $settings['height_unit'];

    $js_settings = [];

    //  Attach all plugin settings to map
    $map->attach_js_settings($map_id);

    $element['map'] = $map->renderMap($map_id, $height);

    // If WKT data input is enabled add field to widget form.
    $element['value'] = [
      '#type' => ($this->getSetting('input')['show']) ? 'textarea' : 'hidden',
      '#title' => t('Geometry data in Well Known Text (WKT) format'),
      '#default_value' => $value,
      '#attributes' => [
        'class' => ['openlayers-geofield-data'],
      ],
    ];

    $element['#attached']['library'][] = 'openlayers/openlayers-widget';

    return $element;
  }

}

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

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