openlayers-8.x-4.x-dev/modules/openlayers_geolocation/src/Plugin/Field/FieldFormatter/OpenlayersGeolocationFormatter.php

modules/openlayers_geolocation/src/Plugin/Field/FieldFormatter/OpenlayersGeolocationFormatter.php
<?php

namespace Drupal\openlayers_geolocation\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldItemListInterface;

use Drupal\openlayers\Plugin\Field\FieldFormatter\OpenlayersFormatterBase;
use Drupal\openlayers\MapSettings;

/**
 * Plugin implementation for an Openlayers Geolocation field formatter.
 *
 * @FieldFormatter(
 *   id = "openlayers_geolocation_formatter",
 *   label = @Translation("Openlayers Map"),
 *   field_types = {
 *     "geolocation"
 *   }
 * )
 */
class OpenlayersGeolocationFormatter extends OpenlayersFormatterBase {

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $entity = $items->getEntity();

    if ($entity->hasTranslation($langcode)) {
      $entity = $entity->getTranslation($langcode);
    }

    $entity_type = $entity->getEntityTypeId();
    $bundle = $entity->bundle();
    $entity_id = $entity->id();
    /* @var \Drupal\Core\Field\FieldDefinitionInterface $field */
    $field = $items->getFieldDefinition();
    $settings = $this->getSettings();
    $map = new MapSettings($settings['map']);
    $cache_tags = $map->cacheTags;
    $map = $map->settings;

    $results = [];
    $features = [];
    foreach ($items as $delta => $item) {
      $features[] = [
        'type' => 'latlon',
        'value' => explode(',', $item->value),
        'style' => [
          'fill' => [
            'color' => 'red',
          ],
          'stroke' => [
            'color' => 'black',
            'width' => 3,
          ],
        ],
      ];
    }
    
    // Add a specific map id.
    $map['id'] = 'openlayers-fieldname-map';

    $js_settings = [
      'map' => $map,
      'features' => $features,
      'settings' => [
        'zoom' => $settings['zoom'],
      ],
    ];
  
//    $results[] = $this->openlayersService->openlayersRenderMap($js_settings, $settings['height'] . $settings['height_unit'], $cache_tags); 
    
    return $results;
  }

}

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

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