openlayers-8.x-4.x-dev/src/Entity/OpenlayersMap.php
src/Entity/OpenlayersMap.php
<?php
namespace Drupal\openlayers\Entity;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityWithPluginCollectionInterface;
use Drupal\openlayers\OpenlayersMapInterface;
use Drupal\openlayers\OpenlayersPluginCollection;
//use Drupal\openlayers\OpenlayersLayerInterface; // TODO - is this needed ?
//use Drupal\openlayers\OpenlayersLayer; // TODO - is this needed ?
use Drupal\openlayers\OpenlayersObjectCollection;
/**
* Defines the Openlayers Map entity.
*
* @ConfigEntityType(
* id = "openlayers_map",
* label = @Translation("Openlayers Map"),
* handlers = {
* "list_builder" = "Drupal\openlayers\Controller\OpenlayersMapListBuilder",
* "form" = {
* "add" = "Drupal\openlayers\Form\OpenlayersMapAddForm",
* "edit" = "Drupal\openlayers\Form\OpenlayersMapEditForm",
* "delete" = "Drupal\openlayers\Form\OpenlayersMapDeleteForm",
* }
* },
* config_prefix = "map",
* admin_permission = "administer openlayers",
* entity_keys = {
* "id" = "id",
* "label" = "label",
* },
* config_export = {
* "id",
* "label",
* "map",
* "layers",
* "styles",
* "controls",
* "interactions",
* "components",
* "map_view",
* "service",
* "pluginId"
* },
* links = {
* "edit-form" = "/admin/config/system/openlayers/map/{openlayers_map}/edit",
* "delete-form" = "/admin/config/system/openlayers/map/{openlayers_map}/delete",
* }
* )
*/
class OpenlayersMap extends ConfigEntityBase implements OpenlayersMapInterface, EntityWithPluginCollectionInterface {
/**
* The Map ID.
*
* @var string
*/
public $id;
/**
* The Map label.
*
* @var string
*/
public $label;
/**
* The factory service.
*
* @var string
*/
public $service;
// Your specific configuration property get/set methods go here,
// implementing the interface.
/**
* The array of layers for this map.
*
* @var array
*/
// protected $layers = [];
public $layers = [];
/**
* The array of styles for this map.
*
* @var array
*/
public $styles = [];
/**
* The array of controls for this map.
*
* @var array
*/
public $controls = [];
/**
* The array of interactions for this map.
*
* @var array
*/
public $interactions = [];
/**
* The ??? flag.?????
*
* @var array ???
*/
public $map_view = [];
/**
* {@inheritdoc}
*/
public function id() {
return $this->id;
}
/**
* {@inheritdoc}
*/
public function label() {
return $this->label;
}
/**
* {@inheritdoc}
*/
public function service() {
return $this->service;
}
/**
* Returns the openlayers plugin manager.
*
* @return \Drupal\Component\Plugin\PluginManagerInterface
* The openlayers plugin manager.
*/
protected function getOpenlayersPluginManager() {
return \Drupal::service('plugin.manager.openlayers');
}
/**
* {@inheritdoc}
*/
public function getPluginCollections() {
// return ['layers' => $this->getLayers(), 'styles' => $this->getStyles(), 'controls' => $this->getControls(), 'interactions' => $this->getInteractions()];
// return ['styles' => $this->getStyles(), 'controls' => $this->getControls(), 'interactions' => $this->getInteractions()];
return [];
}
/**
* {@inheritdoc}
*/
public function getLayer($layer) {
// return $this->getLayers()->get($layer);
return $this->getLayers()[$layer];
}
/**
* {@inheritdoc}
*/
public function getObjects(string $type) {
if (!isset($this->{$type . 'sCollection'})) {
$this->{$type . 'sCollection'} = new OpenlayersObjectCollection($this->{'getOpenlayersPluginManager'}(), ((isset($this->{$type . 's'}) && is_array($this->{$type . 's'})) ? $this->{$type . 's'} : []));
}
return $this->{$type . 'sCollection'};
}
public function getObjects2(string $type) {
if (!isset($this->{$type . 's'}) || !is_array($this->{$type . 's'})) {
$this->{$type . 's'} = [];
}
// TEMP FIX
foreach($this->interactions as $key => $item) {
if ($this->interactions[$key]['uuid'] == '') {
$this->interactions[$key]['uuid'] = $key;
}
if ($this->interactions[$key]['id'] == '') {
$this->interactions[$key]['id'] = 'ol_interaction_dragpan';
}
}
if (!isset($this->{$type . 'sCollection'})) {
$this->{$type . 'sCollection'} = new OpenlayersObjectCollection($this->getInteractionPluginManager(), $this->interactions);
}
return $this->{$type . 'sCollection'};
}
/**
* {@inheritdoc}
*/
public function getAllObjects(string $type) {
$objectNames = \Drupal::service('config.storage')->listAll('openlayers.' . $type);
$objects = [];
foreach ($objectNames as $objectName) {
$objectLabel = \Drupal::config($objectName)->get('label');
$objectName = str_replace('openlayers.' . $type . '.', '', $objectName);
$objects[$objectName] = $objectLabel;
}
return $objects;
}
/**
* {@inheritdoc}
*/
public function getStyle($style) {
return $this->getStyles()->get($style);
}
/**
* {@inheritdoc}
*/
public function getControl($control) {
return $this->getControls()->get($control);
}
/**
* {@inheritdoc}
*/
public function getInteraction($interaction) {
return $this->getInteractions()->get($interaction);
}
/**
* {@inheritdoc}
*/
public function addMapObject($configuration, $type) {
$configuration['uuid'] = \Drupal::service('uuid')->generate();
$this->{$type . 's'}[$configuration['uuid']] = $configuration;
return $this->{$type . 's'};
}
/**
* {@inheritdoc}
*/
public function addMapStyle($configuration) {
$configuration['uuid'] = $this->uuidGenerator()->generate();
$this->getStyles()->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
/**
* {@inheritdoc}
*/
public function addMapControl($configuration) {
$configuration['uuid'] = $this->uuidGenerator()->generate();
$this->getControls()->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
/**
* {@inheritdoc}
*/
public function addMapInteraction($configuration) {
$configuration['uuid'] = $this->uuidGenerator()->generate();
$this->getInteractions()->addInstanceId($configuration['uuid'], $configuration);
return $configuration['uuid'];
}
/**
* {@inheritdoc}
*/
public function updateMapObject($configuration, $type) {
$this->{$type . 's'}[$configuration['uuid']] = $configuration;
}
/**
* {@inheritdoc}
*/
public function deleteMapLayer($configuration) {
$uuid = $configuration->uuid;
unset($this->layers[$uuid]);
}
/**
* {@inheritdoc}
*/
public function deleteMapStyle($configuration) {
$uuid = $configuration->uuid;
unset($this->styles[$uuid]);
}
/**
* {@inheritdoc}
*/
public function deleteMapControl($configuration) {
$uuid = $configuration->uuid;
unset($this->controls[$uuid]);
}
/**
* {@inheritdoc}
*/
public function deleteMapInteraction($configuration) {
$uuid = $configuration->uuid;
unset($this->interactions[$uuid]);
}
/**
* {@inheritdoc}
*/
public function deleteMapComponent($configuration) {
$uuid = $configuration->uuid;
unset($this->components[$uuid]);
}
/**
* {@inheritdoc}
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if ($update) {
if (!empty($this->original) && $this->id() !== $this->original->id()) {
// The old image interaction name needs flushing after a rename.
$this->original->flush();
}
else {
// Flush image interaction when updating without changing the name.
$this->flush();
}
}
}
/**
* {@inheritdoc}
*/
public function flush() {
// Clear caches so that formatters may be added for this interaction.
Cache::invalidateTags($this->getCacheTagsToInvalidate());
return $this;
}
}
