You are here

public function GeolocationGoogleMapInput::processMapInputElement in Geolocation Field 8

Mapping input.

Parameters

array $element: Element.

Return value

array Renderable form element.

File

src/Element/GeolocationGoogleMapInput.php, line 51

Class

GeolocationGoogleMapInput
Provides a one-line text field form element.

Namespace

Drupal\geolocation\Element

Code

public function processMapInputElement(array &$element) {
  $element['#tree'] = TRUE;
  $max_locations = 1;
  if (!empty($element['#max_locations'])) {
    $max_locations = (int) $element['#max_locations'];
  }
  if (!empty($element['#locations'])) {
    $element['#locations'] = array_splice($element['#locations'], 0, $max_locations);
  }
  $element['geolocation_map'] = $this
    ->preRenderGoogleMapElement($element);
  $element['#theme_wrappers']['container']['#attributes']['id'] = 'geolocation-google-map-form-element-' . $element['geolocation_map']['#uniqueid'];
  $element['#attached']['drupalSettings'] = [
    'geolocation' => [
      'googleMapFormElements' => [
        $element['geolocation_map']['#uniqueid'] => [
          'maxLocations' => $max_locations,
        ],
      ],
    ],
  ];
  for ($i = 0; $i < $max_locations; $i++) {
    $element['geolocation_map_input_' . $i] = [
      '#type' => 'fieldset',
      '#attributes' => [
        'class' => [
          'geolocation-map-input',
          'geolocation-map-input-' . $i,
        ],
      ],
      'latitude' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Latitude'),
        '#attributes' => [
          'class' => [
            'geolocation-map-input-latitude',
          ],
        ],
        '#value' => empty($element['#locations'][$i]) ? '' : $element['#locations'][$i]['latitude'],
      ],
      'longitude' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Longitude'),
        '#attributes' => [
          'class' => [
            'geolocation-map-input-longitude',
          ],
        ],
        '#value' => empty($element['#locations'][$i]) ? '' : $element['#locations'][$i]['longitude'],
      ],
    ];
    if (!empty($element['#title'])) {
      if ($max_locations > 1) {
        $element['geolocation_map_input_' . $i]['#title'] = $element['#title'] . ' #' . $i;
      }
      else {
        $element['geolocation_map_input_' . $i]['#title'] = $element['#title'];
      }
    }
  }
  return $element;
}