You are here

public function LeafletDefaultWidget::formElement in Leaflet 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/LeafletDefaultWidget.php \Drupal\leaflet\Plugin\Field\FieldWidget\LeafletDefaultWidget::formElement()
  2. 2.1.x src/Plugin/Field/FieldWidget/LeafletDefaultWidget.php \Drupal\leaflet\Plugin\Field\FieldWidget\LeafletDefaultWidget::formElement()

File

src/Plugin/Field/FieldWidget/LeafletDefaultWidget.php, line 359

Class

LeafletDefaultWidget
Plugin implementation of the "leaflet_widget" widget.

Namespace

Drupal\leaflet\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
  $element = parent::formElement($items, $delta, $element, $form, $form_state);
  $settings = $this
    ->getSettings();

  // Determine map settings and add map element.
  $map_settings = $this
    ->getSetting('map');
  $default_settings = self::defaultSettings();
  $input_settings = $this
    ->getSetting('input');
  $js_settings = [];
  $map = leaflet_map_get_info($map_settings['leaflet_map'] ?? $default_settings['map']['leaflet_map']);
  $map['context'] = 'widget';

  // Get token context.
  $token_context = [
    'field' => $items,
    $this->fieldDefinition
      ->getTargetEntityTypeId() => $items
      ->getEntity(),
  ];

  // Extend options to reset_map and geocoder to uniform with Leafket
  // Formatter and Leaflet View processing.
  $options = array_merge($map_settings, [
    'reset_map' => $this
      ->getSetting('reset_map'),
  ], [
    'path' => $this
      ->getSetting('path'),
  ], [
    'geocoder' => $this
      ->getSetting('geocoder'),
  ]);

  // Set Map additional map Settings.
  $this
    ->setAdditionalMapOptions($map, $options);

  // Attach class to wkt input element, so we can find it in js.
  $json_element_name = 'leaflet-widget-input';
  $element['value']['#attributes']['class'][] = $json_element_name;

  // Set the readonly for styling, if readonly.
  if (isset($input_settings["readonly"]) && $input_settings["readonly"]) {
    $element['value']['#attributes']['class'][] = "readonly";
  }
  if (!empty($map_settings['locate'])) {
    $js_settings['locate'] = TRUE;
    unset($map['settings']['center']);
  }
  $element['map'] = $this->leafletService
    ->leafletRenderMap($map, [], $map_settings['height'] . 'px');
  $element['map']['#weight'] = -1;
  $element['title']['#type'] = 'item';
  $element['title']['#title'] = $element['value']['#title'];
  $element['title']['#weight'] = -2;
  $element['value']['#title'] = $this
    ->t('GeoJson Data');

  // Build JS settings for leaflet widget.
  $js_settings['map_id'] = $element['map']['#map_id'];
  $js_settings['jsonElement'] = '.' . $json_element_name;
  $cardinality = $items
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getCardinality();
  $js_settings['multiple'] = $cardinality == 1 ? FALSE : TRUE;
  $js_settings['cardinality'] = $cardinality > 0 ? $cardinality : 0;
  $js_settings['autoCenter'] = $map_settings['auto_center'] ?? $default_settings['auto_center'];
  $js_settings['inputHidden'] = empty($input_settings['show']);
  $js_settings['inputReadonly'] = !empty($input_settings['readonly']);
  $js_settings['toolbarSettings'] = $this
    ->getSetting('toolbar') ?? $default_settings['toolbar'];
  $js_settings['scrollZoomEnabled'] = !empty($map_settings['scroll_zoom_enabled']) ? $map_settings['scroll_zoom_enabled'] : FALSE;
  $js_settings['path'] = str_replace([
    "\n",
    "\r",
  ], "", $this->token
    ->replace($this
    ->getSetting('path'), $token_context));
  $js_settings['geocoder'] = $this
    ->getSetting('geocoder');
  $js_settings['map_position'] = $map_settings['map_position'] ?? [];

  // Leaflet.widget plugin.
  $element['map']['#attached']['library'][] = 'leaflet/leaflet-widget';

  // Settings and geo-data are passed to the widget keyed by field id.
  $element['map']['#attached']['drupalSettings']['leaflet_widget'][$element['map']['#map_id']] = $js_settings;

  // Convert default value to geoJSON format.
  if ($geom = $this->geoPhpWrapper
    ->load($element['value']['#default_value'])) {
    $element['value']['#default_value'] = $geom
      ->out('json');
  }
  return $element;
}