You are here

public function LeafletGeojson::blockForm in Leaflet GeoJSON 8

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/LeafletGeojson.php, line 83

Class

LeafletGeojson
Provides a 'Leaflet Geojson' Block.

Namespace

Drupal\leaflet_geojson\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $conf = $this->configuration;

  // Build base layer selector.
  $base_options = [];
  foreach (leaflet_map_get_info() as $key => $map) {
    $base_options[$key] = $this
      ->t('@label', [
      '@label' => $map['label'],
    ]);
  }

  // The default selection is the first one, or the previously selected one.
  $default_base = key($base_options);
  if (isset($conf['map_settings']['base'])) {
    $default_base = $conf['map_settings']['base'];
  }
  $form['map_settings']['base'] = [
    '#title' => $this
      ->t('Leaflet base layer'),
    '#type' => 'select',
    '#options' => $base_options,
    '#default_value' => $default_base,
    '#required' => TRUE,
    '#description' => $this
      ->t('Select the Leaflet base layer (map style) that will display the data.'),
  ];

  // Provide some UI help for setting up multi-layer maps.
  $data_layers_description = $this
    ->t('Choose one or more GeoJSON sources that will provide the map data.') . ' ';
  $data_layers_description .= $this
    ->t('If more than one source  is selected, a layer control will appear on the map.') . ' ';
  $data_layers_description .= $this
    ->t('Views GeoJSON page displays are automatically exposed here.');

  // Build the data layers selector.
  $form['map_settings']['info'] = [
    '#type' => 'fieldset',
    '#tree' => TRUE,
    '#title' => 'Views GeoJSON Data Layers',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#description' => $data_layers_description,
  ];

  // Grab the available layers.
  $sources = leaflet_geojson_source_get_info(NULL, TRUE);
  $source_options = [];
  foreach ($sources as $id => $source) {
    $source_options[$id] = $source['title'];
  }

  // Figure out if we have any data layers yet, and set the layer count.
  if (empty($form_state
    ->get('layer_count'))) {

    // During creation, we wont have any data yet, so only one layer.
    if (!isset($conf['map_settings']['info']['data'])) {
      $form_state
        ->set('layer_count', 1);
    }
    else {

      // During edit, we'll have one or more layers, so count.
      $form_state
        ->set('layer_count', count($conf['map_settings']['info']['data']));
    }
  }

  // Build the number of layer selections indicated by layer_count.
  for ($layer_index = 1; $layer_index <= $form_state
    ->get('layer_count'); $layer_index++) {
    $default_layer_source = key($source_options);
    if (isset($conf['map_settings']['info']['data']['leaflet_' . $layer_index])) {
      $default_layer_source = $conf['map_settings']['info']['data']['leaflet_' . $layer_index];
    }
    $form['map_settings']['info']['data']['leaflet_' . $layer_index] = [
      '#type' => 'select',
      '#title' => $this
        ->t('GeoJSON layer source'),
      '#options' => $source_options,
      '#default_value' => $default_layer_source,
      '#required' => $layer_index == 1,
    ];
  }

  // Provide an "Add another layer" button.
  $form['map_settings']['info']['add_layer'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add another layer'),
    '#submit' => [
      'leaflet_geojson_map_pane_add_layer',
    ],
  ];

  // Provide a "Remove" button for latest selected layer.
  if ($form_state
    ->get('layer_count') > 1) {
    $form['map_settings']['info']['remove_layer'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Remove last layer'),
      '#submit' => [
        'leaflet_geojson_map_pane_remove_last_layer',
      ],
    ];
  }

  // Leaflet wants a height in the call to the render function.
  $default_height = isset($conf['map_settings']['height']) ? $conf['map_settings']['height'] : 400;
  $form['map_settings']['height'] = [
    '#title' => $this
      ->t('Map height'),
    '#type' => 'textfield',
    '#size' => 4,
    '#default_value' => $default_height,
    '#required' => FALSE,
    '#description' => $this
      ->t("Set the map height"),
  ];

  // Added to have 100% in height.
  $form['map_settings']['height_unit'] = [
    '#title' => $this
      ->t('Map height unit'),
    '#type' => 'select',
    '#options' => [
      'px' => $this
        ->t('px'),
      '%' => $this
        ->t('%'),
    ],
    '#default_value' => isset($conf['map_settings']['height_unit']) ? $conf['map_settings']['height_unit'] : 'px',
    '#description' => $this
      ->t('Wether height is absolute (pixels) or relative (percent).'),
  ];

  // Optionally override natural center and zoom.
  $default_override_zoom_center = isset($conf['map_settings']['override_zoom_center']) && $conf['map_settings']['override_zoom_center'];
  $form['map_settings']['override_zoom_center'] = [
    '#type' => 'checkbox',
    '#title' => 'Override natural center and zoom placement',
    '#default_value' => $default_override_zoom_center,
    '#description' => $this
      ->t("Map will auto zoom and center based on the data. Check this box to customize the zooom and center"),
  ];
  $form['map_settings']['custom_zoom_center'] = [
    '#type' => 'fieldset',
    '#title' => 'Zoom and Center',
    '#tree' => TRUE,
    '#states' => [
      'visible' => [
        ':input[name="override_zoom_center"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $default_zoom = isset($conf['map_settings']['custom_zoom_center']['zoom']) ? $conf['map_settings']['custom_zoom_center']['zoom'] : 1;
  $form['map_settings']['custom_zoom_center']['zoom'] = [
    '#title' => $this
      ->t('Zoom'),
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $default_zoom,
    '#required' => FALSE,
  ];
  $form['map_settings']['custom_zoom_center']['center'] = [
    '#type' => 'fieldset',
    '#title' => 'Map center',
    '#tree' => TRUE,
    '#description' => $this
      ->t("Provide a default map center especially when using the bounding box strategy."),
  ];
  $default_center = isset($conf['map_settings']['custom_zoom_center']['center']) ? $conf['map_settings']['custom_zoom_center']['center'] : [
    'lon' => 0,
    'lat' => 0,
  ];
  $form['map_settings']['custom_zoom_center']['center']['lon'] = [
    '#title' => $this
      ->t('Center longitude'),
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $default_center['lon'],
    '#required' => FALSE,
  ];
  $form['map_settings']['custom_zoom_center']['center']['lat'] = [
    '#title' => $this
      ->t('Center latitude'),
    '#type' => 'textfield',
    '#size' => 20,
    '#default_value' => $default_center['lat'],
    '#required' => FALSE,
  ];
  return $form;
}