View source
<?php
namespace Drupal\openlayers\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Cache\Cache;
use Drupal\openlayers\OpenlayersConfigurablePluginInterface;
use Drupal\openlayers\OpenlayersMapInterface;
abstract class OpenlayersPluginFormBase extends FormBase {
protected $olMap;
protected $olPlugin;
public function getFormId() {
return 'openlayers_plugin_form';
}
public function buildForm(array $form, FormStateInterface $form_state, OpenlayersMapInterface $map = NULL, $plugin_type = NULL, $plugin = NULL) {
$this->olMap = $map;
try {
$this->olPlugin = $this
->preparePlugin($plugin_type, $plugin);
} catch (PluginNotFoundException $e) {
throw new NotFoundHttpException("Invalid plugin id: '{$plugin}'.");
}
$definition = $this->olPlugin
->getPluginDefinition();
$request = $this
->getRequest();
if (!$this->olPlugin instanceof OpenlayersConfigurablePluginInterface) {
throw new NotFoundHttpException();
}
$form['#attached']['library'][] = 'image/admin';
$form['uuid'] = [
'#type' => 'value',
'#value' => $this->olPlugin
->getUuid(),
];
$form['id'] = [
'#type' => 'value',
'#value' => $this->olPlugin
->getPluginId(),
];
$form['data'] = [];
$subform_state = SubformState::createForSubform($form['data'], $form, $form_state);
$form['data'] = $this->olPlugin
->buildConfigurationForm($form['data'], $subform_state);
$form['data']['#tree'] = TRUE;
$form['weight'] = [
'#type' => 'hidden',
'#value' => isset($this->olPlugin->ol_id) ? $this->olPlugin->ol_id : $this->olPlugin
->getWeight(),
];
$form['ol_id'] = [
'#type' => 'hidden',
'#value' => isset($definition['ol_id']) ? $definition['ol_id'] : '???',
];
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#button_type' => 'primary',
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#url' => $this->olMap
->toUrl('edit-form'),
'#attributes' => [
'class' => [
'button',
],
],
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$this->olPlugin
->validateConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$form_state
->cleanValues();
$this->olPlugin
->submitConfigurationForm($form['data'], SubformState::createForSubform($form['data'], $form, $form_state));
$this->olPlugin
->setWeight($form_state
->getValue('weight'));
if (!$this->olPlugin
->getUuid()) {
$definition = $this->olPlugin
->getPluginDefinition();
$configuration = $this->olPlugin
->getConfiguration();
$configuration['ol_id'] = $form_state
->getValue('ol_id');
$plugin_type = $definition['type'];
$this->olMap
->{'addMap' . ucwords($plugin_type)}($configuration);
}
$this->olMap
->save();
Cache::invalidateTags([
'config:openlayers.map.ol_map2',
]);
$this
->messenger()
->addStatus($this
->t('The Settings for the plugin were successfully saved.'));
$form_state
->setRedirectUrl($this->olMap
->toUrl('edit-form'));
}
protected abstract function preparePlugin($plugin_type, $plugin);
}