You are here

public function Map::getForm in Search API Location 8

Returns a form to configure settings for the plugin.

Parameters

array $form: The form where the settings form is being included in.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

array $options: Option array with extra info of the plugin.

Return value

array The form definition for the widget settings.

Overrides LocationInputPluginBase::getForm

File

src/Plugin/search_api_location/location_input/Map.php, line 103

Class

Map
Represents the Raw Location Input.

Namespace

Drupal\search_api_location\Plugin\search_api_location\location_input

Code

public function getForm(array $form, FormStateInterface $form_state, array $options) {
  $form['value']['#tree'] = TRUE;

  // Get the default values for existing field.
  $lat_default_value = (double) $form_state
    ->getUserInput()[$options['expose']['identifier']]['lat'] ?: "51.037932";
  $lng_default_value = (double) $form_state
    ->getUserInput()[$options['expose']['identifier']]['lng'] ?: "3.712105";
  $radius_default_value = (double) $form_state
    ->getUserInput()[$options['expose']['identifier']]['distance']['from'] ?: "1000";
  $id = $options['id'];

  // Hidden lat,lng input fields.
  $form['value']['lat'] = [
    '#type' => 'hidden',
    '#default_value' => $lat_default_value,
    '#attributes' => [
      'id' => [
        "sal-{$id}-lat",
      ],
    ],
  ];
  $form['value']['lng'] = [
    '#type' => 'hidden',
    '#default_value' => $lng_default_value,
    '#attributes' => [
      'id' => [
        "sal-{$id}-lng",
      ],
    ],
  ];
  $form['value']['distance']['from'] = [
    '#type' => 'hidden',
    '#default_value' => $radius_default_value,
    '#attributes' => [
      'id' => [
        "sal-{$id}-radius",
      ],
    ],
  ];

  // Add the map container.
  $form['value']['map'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'id' => "sal-{$id}-map",
      'class' => "sal-map",
    ],
    '#attached' => [
      'library' => [
        'search_api_location/location-picker',
      ],
      'drupalSettings' => [
        'search_api_location' => [
          $id => [
            'id' => $id,
            'lat' => (double) $lat_default_value,
            'lng' => (double) $lng_default_value,
            'radius' => $radius_default_value,
            'radius_border_color' => $options['plugin-geocode_map']['radius_border_color'],
            'radius_border_weight' => $options['plugin-geocode_map']['radius_border_weight'],
            'radius_background_color' => $options['plugin-geocode_map']['radius_background_color'],
            'radius_background_transparency' => $options['plugin-geocode_map']['radius_background_transparency'],
            'marker_image' => $options['plugin-geocode_map']['marker_image'],
          ],
        ],
      ],
    ],
  ];
  $form['value']['slider'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#attributes' => [
      'id' => "sal-{$id}-slider",
      'class' => "sal-slider",
    ],
  ];
  return $form;
}