Repository URL to install this package:
|
Version:
1.0.4 ▾
|
<?php
namespace Drupal\premium_maps;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheFactory;
use Drupal\Core\Cache\CacheFactoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
class MapsStorage {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
* The entity type manger object.
*/
private $entityTypeManager;
/**
* @var \Drupal\Core\Messenger\MessengerInterface
* The messenger service for sending messages to the user.
*/
private $messenger;
/**
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $config;
/**
* @var \Drupal\Core\Cache\CacheFactory
*/
private $cache;
/**
* Language manager.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* MapsRepository constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManager $entityTypeManager
* The entity injected entity type manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* @param \Drupal\Core\Cache\CacheFactoryInterface $cacheFactory
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
*/
public function __construct(
EntityTypeManager $entityTypeManager,
ConfigFactoryInterface $configFactory,
CacheFactoryInterface $cacheFactory,
MessengerInterface $messenger,
LanguageManagerInterface $languageManager
) {
$this->entityTypeManager = $entityTypeManager;
$this->config = $configFactory->get('premium_maps.map_settings');
$this->cache = $cacheFactory->get('default');
$this->messenger = $messenger;
$this->languageManager = $languageManager;
}
/**
* Get maps pins.
*
* @param string $entity_type_id
* The type of marker to get.
*
* @param string $bundle
* The bundle type to get.
*
* @param string $field_name
* The field name of the field containing the location coordinates.
*
* @return array
* Array containing all the markers of the defined type.
*/
public function getMarkers(string $entity_type_id, string $bundle, string $field_name) {
$current_language = $this->languageManager->getCurrentLanguage()->getId();
// Check if markers is already in cache.
$cached = $this->cache->get('map_markers_' . $current_language);
if ($cached) {
return $cached->data;
}
try {
/** @var \Drupal\node\Entity\Node[] $markers */
$entities = $this->entityTypeManager->getStorage($entity_type_id)->loadByProperties(['type' => $bundle]);
$markers = [];
$tags = [];
foreach($entities as $entity) {
// Enable translation.
if ($entity && $entity->hasTranslation($current_language)) {
$entity = $entity->getTranslation($current_language);
}
$markers[] = new Marker($entity);
$tags[] = $entity->getCacheTags();
}
// Merge the cache tags into on one dimensional array.
$cache_tags = (array_merge([], ...$tags));
// Add a cache tag to clear the marker cache when creating new markers
$cache_tags[] = 'premium_maps:markers';
// Set the cache
$this->cache->set('map_markers_' . $current_language, $markers,Cache::PERMANENT, $cache_tags);
return $markers;
}
catch (InvalidPluginDefinitionException $e) {
$this->messenger->addError($e->getMessage());
}
catch (PluginNotFoundException $e) {
$this->messenger->addError($e->getMessage());
}
return [];
}
/**
* Get all markers defined in config.
*
* @return array
* Array containing all the markers of the defined the map settings.
*/
public function getMarkersFromConfig() {
$bundles = $this->config->get('maps_config');
$markers = [];
foreach ($bundles as $bundle => $item) {
$markers += $this->getMarkers('node', $bundle, $item['field']);
}
return $markers;
}
}