You are here

public function GeolocationBlock::blockForm in Geolocation Field 8.2

Same name and namespace in other branches
  1. 8.3 src/Plugin/Block/GeolocationBlock.php \Drupal\geolocation\Plugin\Block\GeolocationBlock::blockForm()

Overrides BlockPluginTrait::blockForm

File

src/Plugin/Block/GeolocationBlock.php, line 94

Class

GeolocationBlock
Exposes a map rendered as a block.

Namespace

Drupal\geolocation\Plugin\Block

Code

public function blockForm($form, FormStateInterface $form_state) {
  $form = parent::blockForm($form, $form_state);
  $form['locations'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Markers'),
    '#attributes' => [
      'id' => 'block-locations',
    ],
  ];
  for ($i = 0; $i < count($this->configuration['locations']); $i++) {
    $form['locations'][$i] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t('Marker %index', [
        '%index' => $i,
      ]),
      'marker_title' => [
        '#type' => 'textfield',
        '#title' => $this
          ->t('Marker title'),
        '#description' => $this
          ->t('When the cursor hovers on the marker, this title will be shown as description.'),
        '#default_value' => empty($this->configuration['locations'][$i]['marker_title']) ? '' : $this->configuration['locations'][$i]['marker_title'],
      ],
      'marker_content' => [
        '#type' => 'text_format',
        '#title' => $this
          ->t('Marker info text'),
        '#description' => $this
          ->t('When the marker is clicked, this text will be shown in a popup above it. Leave blank to not display. Token replacement supported.'),
      ],
      'marker_coordinates' => [
        '#type' => 'geolocation_input',
        '#title' => t('Marker Coordinates'),
        '#default_value' => empty($this->configuration['locations'][$i]['marker_coordinates']) ? '' : $this->configuration['locations'][$i]['marker_coordinates'],
      ],
    ];
    if (!empty($this->configuration['locations'][$i]['marker_content']['value'])) {
      $form['locations'][$i]['marker_content']['#default_value'] = $this->configuration['locations'][$i]['marker_content']['value'];
    }
    if (!empty($this->configuration['locations'][$i]['marker_content']['format'])) {
      $form['locations'][$i]['marker_content']['#format'] = $this->configuration['locations'][$i]['marker_content']['format'];
    }
  }
  $form['locations']['add_item'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add location'),
    '#submit' => [
      [
        $this,
        'addLocationSubmit',
      ],
    ],
    '#ajax' => [
      'callback' => [
        $this,
        'addLocation',
      ],
      'wrapper' => 'block-locations',
      'effect' => 'fade',
    ],
  ];
  $map_provider_options = $this->mapProviderManager
    ->getMapProviderOptions();
  if (empty($map_provider_options)) {
    return [
      '#type' => 'html_tag',
      '#tag' => 'span',
      '#value' => t("No map provider found."),
    ];
  }
  $form['centre'] = $this->mapCenterManager
    ->getCenterOptionsForm((array) $this->configuration['centre'], [
    'formatter' => $this,
  ]);
  $form['map_provider_id'] = [
    '#type' => 'select',
    '#options' => $map_provider_options,
    '#title' => $this
      ->t('Map Provider'),
    '#default_value' => $this->configuration['map_provider_id'],
    '#ajax' => [
      'callback' => [
        get_class($this->mapProviderManager),
        'addSettingsFormAjax',
      ],
      'wrapper' => 'map-provider-settings',
      'effect' => 'fade',
    ],
  ];
  $form['map_provider_settings'] = [
    '#type' => 'html_tag',
    '#tag' => 'span',
    '#value' => t("No settings available."),
  ];
  $parents = [
    'settings',
  ];
  $map_provider_id = NestedArray::getValue($form_state
    ->getUserInput(), array_merge($parents, [
    'map_provider_id',
  ]));
  if (empty($map_provider_id)) {
    $map_provider_id = $this->configuration['map_provider_id'];
  }
  if (empty($map_provider_id)) {
    $map_provider_id = key($map_provider_options);
  }
  $map_provider_settings = NestedArray::getValue($form_state
    ->getUserInput(), array_merge($parents, [
    'map_provider_settings',
  ]));
  if (empty($map_provider_settings)) {
    $map_provider_settings = $this->configuration['map_provider_settings'];
  }
  if (!empty($map_provider_id)) {
    $form['map_provider_settings'] = $this->mapProviderManager
      ->createInstance($map_provider_id, $map_provider_settings)
      ->getSettingsForm($map_provider_settings, array_merge($parents, [
      'map_provider_settings',
    ]));
  }
  $form['map_provider_settings'] = array_replace($form['map_provider_settings'], [
    '#prefix' => '<div id="map-provider-settings">',
    '#suffix' => '</div>',
  ]);
  return $form;
}