openlayers-8.x-4.x-dev/src/MapComponent.php
src/MapComponent.php
<?php
namespace Drupal\openlayers;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\SubformState;
/**
* Contains details of how a component is joined to a map.
*
*/
class MapComponent {
public $configuration;
public function __construct($configuration, $action) {
if ($action == 'update' || $action == 'delete') {
$this->id = $configuration['id'];
$this->uuid = $configuration['uuid'];
$this->configuration = isset($configuration['data']) ? $configuration['data'] : [];
$this->plugin = $this->getPlugin();
} else {
$this->id = $configuration;
$this->uuid = \Drupal::service('uuid')->generate();
$this->configuration = [];
$this->plugin = $this->getPlugin();
}
}
/**
* {@inheritdoc}
*/
public function id() {
return $this->id;
}
/**
* {@inheritdoc}
*/
public function label() {
return \Drupal::config('openlayers.component.' . $this->id)->get('label');
}
/**
* {@inheritdoc}
*/
public function getUuid() {
return $this->uuid;
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return [
'uuid' => $this->getUuid(),
'id' => $this->id(),
// 'weight' => $this->getWeight(),
'data' => $this->configuration,
];
}
/**
* {@inheritdoc}
*/
public function getPlugin() {
$component = Openlayers::load('component', $this->id);
return $component;
}
/**
* {@inheritdoc}
*/
/*
public function getSummary() {
$summary = [
'#theme' => 'image_rotate_summary',
'#data' => $this->configuration,
];
// $summary += parent::getSummary();
return $summary;
}
*/
/**
* {@inheritdoc}
*/
public function defaultConfiguration() { // TODO: Where is this called from ?
return [
/*
'base' => 'base',
'title' => 'kkkkkk',
'visible' => 0,
*/
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
// Include subform containing plugin configuration options;
if (array_key_exists('options', $this->configuration)) {
$form['data']['options'] = $this->configuration['options'];
} else {
$form['data'] = [];
}
$subform_state = SubformState::createForSubform($form['data'], $form, $form_state);
$form['data'] = $this->plugin->buildConfigurationForm($form['data'], $subform_state);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
/*
if (!$form_state->isValueEmpty('bgcolor') && !Color::validateHex($form_state->getValue('bgcolor'))) {
$form_state->setErrorByName('bgcolor', $this->t('Background color must be a hexadecimal color value.'));
}
*/
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['options'] = $form_state->getValue('data')['options'];
}
}
