Repository URL to install this package:
|
Version:
1.0.4 ▾
|
<?php
namespace Drupal\premium_maps;
use Drupal\Core\Entity\EntityInterface;
class Marker {
private static $settings;
/** @var \Drupal\Core\Entity\EntityInterface $entity */
private $entity;
public function __construct(EntityInterface $entity) {
$this->entity = $entity;
$this->title = $entity->label();
}
// Retrieve the map settings
protected function getSettings() {
if (!empty(self::$settings)) {
return self::$settings;
}
self::$settings = \Drupal::config('premium_maps.map_settings');
return self::$settings;
}
/**
* Return the maker as an array.
*
* @return array
* Array containing the marker.
*/
public function toArray(): array {
$content = $this->getContent();
return [
'location' => [
'lat' => $this->getLat(),
'long' => $this->getLon(),
],
'title' => $this->getTitle(),
'content' => \Drupal::service('renderer')->render($content),
'icon' => $this->getIcon(),
'category' => $this->getCategoryId(),
];
}
/**
* Return the name of the field containing the coordinates.
*
* @return mixed|null
*/
private function getCoordinatesField() {
$settings = $this->getSettings();
return $settings->get('maps_config')[$this->entity->bundle()]['field'] ??
NULL;
}
/**
* The the marker title.
*
* @return string
* The title.
*/
public function getTitle(): string {
return $this->entity->label();
}
/**
* Get the markers latitude.
*
* @return mixed
* The latitude.
*/
public function getLat() {
return $this->entity->get($this->getCoordinatesField())->lat;
}
/**
* Get the markers longitude.
*
* @return mixed
* The longitude.
*/
public function getLon() {
return $this->entity->get($this->getCoordinatesField())->lon;
}
/**
* Get the content on the entity.
*
* The view mode map in used.
*
* @return array
* The content.
*/
public function getContent() {
$view_builder = \Drupal::entityTypeManager()
->getViewBuilder($this->entity->getEntityTypeId());
try {
return $view_builder->view(
$this->entity, 'map', $this->entity->language()->getId()
);
} catch (\Exception $e) {
\Drupal::logger('map')->error($e->getMessage());
}
return [];
}
/**
* Get the icon defined on the term.
*
* @return mixed
* The icon
*/
public function getIcon() {
if ($this->entity->hasField('map_marker_type')
&& !$this->entity->get('map_marker_type')->isEmpty()
) {
return $this->entity->get('map_marker_type')
->entity->get('field_marker_map_icon')->value;
}
return NULL;
}
/**
* Get the icon defined on the term.
*
* @return mixed
* The category id.
*/
public function getCategoryId() {
if ($this->entity->hasField('map_marker_type')) {
return $this->entity->get('map_marker_type')->target_id;
}
return NULL;
}
}