You are here

public function GeofieldMapWidget::formElement in Geofield Map 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldWidget/GeofieldMapWidget.php \Drupal\geofield_map\Plugin\Field\FieldWidget\GeofieldMapWidget::formElement()

Implements \Drupal\field\Plugin\Type\Widget\WidgetInterface::formElement().

Overrides GeofieldLatLonWidget::formElement

File

src/Plugin/Field/FieldWidget/GeofieldMapWidget.php, line 719

Class

GeofieldMapWidget
Plugin implementation of the 'geofield_map' widget.

Namespace

Drupal\geofield_map\Plugin\Field\FieldWidget

Code

public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {

  /* @var \Drupal\geofield\Plugin\Field\FieldType\GeofieldItem $geofield_item */
  $geofield_item = $items
    ->getValue()[$delta];
  if (empty($geofield_item) || $geofield_item['geo_type'] == 'Point') {
    $gmap_api_key = $this
      ->getGmapApiKey();
    $latlon_value = [];
    foreach ($this->components as $component) {
      $latlon_value[$component] = isset($items[$delta]->{$component}) ? floatval($items[$delta]->{$component}) : $this
        ->getSetting('default_value')[$component];
    }
    $element += [
      '#type' => 'geofield_map',
      '#gmap_api_key' => $gmap_api_key,
      '#gmap_places' => (int) $this
        ->getSetting('map_google_places')['places_control'],
      '#gmap_places_options' => $this
        ->getSetting('map_google_places')['places_additional_options'],
      '#gmap_geocoder' => (int) $this
        ->getSetting('map_geocoder')['control'],
      '#gmap_geocoder_settings' => $this->geofieldGmapGeocoder
        ->getJsGeocoderSettings($this
        ->getSetting('map_geocoder')['settings']),
      '#default_value' => $latlon_value,
      '#geolocation' => $this
        ->getSetting('html5_geolocation'),
      '#geofield_map_geolocation_override' => $this
        ->getSetting('html5_geolocation'),
      '#map_library' => $this
        ->getSetting('map_library'),
      '#map_type' => 'leaflet' === $this
        ->getSetting('map_library') ? $this
        ->getSetting('map_type_leaflet') : $this
        ->getSetting('map_type_google'),
      '#map_type_selector' => $this
        ->getSetting('map_type_selector'),
      '#map_types_google' => $this->gMapTypesOptions,
      '#map_types_leaflet' => $this->leafletTileLayers,
      '#map_dimensions' => $this
        ->getSetting('map_dimensions'),
      '#zoom' => $this
        ->getSetting('zoom'),
      '#click_to_find_marker' => $this
        ->getSetting('click_to_find_marker'),
      '#click_to_place_marker' => $this
        ->getSetting('click_to_place_marker'),
      '#hide_coordinates' => $this
        ->getSetting('hide_coordinates'),
      '#geoaddress_field' => $this
        ->getSetting('geoaddress_field'),
      '#error_label' => !empty($element['#title']) ? $element['#title'] : $this->fieldDefinition
        ->getLabel(),
    ];
  }
  else {
    $element += [
      '#prefix' => '<div class="geofield-map-warning">' . $this
        ->t('This Geofield Map cannot be applied because Polylines and Polygons are not supported at the moment') . '</div>',
      '#type' => 'textarea',
      '#default_value' => $items[$delta]->value ?: NULL,
    ];
  }
  return [
    'value' => $element,
  ];
}