custom dynamic sites svg map

This commit is contained in:
2024-10-01 21:57:47 +02:00
parent 84f02ff73e
commit f115c87ad3
19 changed files with 471 additions and 35 deletions

View File

@@ -0,0 +1,113 @@
<?php
namespace Drupal\q2d_mod\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\Attribute\Block;
// use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
/**
* Provides a 'Sites Map' Block.
* @Block(
* id = "sitesmap_block",
* admin_label = @Translation("Sites map Block"),
* )
*/
class SitesMap extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$return = null;
$allSites = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'site', 'status' => 1]);
$sites_paths = "";
$vp_w = 400;
$vp_h = 400;
// Coordonnées géographiques des coins de la carte (France)
$latTop = 52.0; // Nord-Ouest (coin supérieur gauche)
$lonLeft = -6.0;
$latBottom = 40.0; // Sud-Est (coin inférieur droit)
$lonRight = 11.0;
foreach($allSites as $index => $site){
$title = $site->get('title')->getString();
$subtitle = $site->get('field_sous_titre')->getString();
$link_options = ['absolute' => FALSE, 'attributes' => ['class' => 'site-link']];
$site_link_object = Link::createFromRoute("voir le site", 'entity.node.canonical', ['node' => $site->id()], $link_options);
$link = $site_link_object->toString()->getGeneratedLink();
$datacontent = htmlspecialchars("<strong>$title</strong><br>$subtitle<br>$link");
$geofield = $site->get('field_geofield')->get(0);
$lon = $geofield->lon;
$lat = $geofield->lat;
$x = round(($lon - $lonLeft) / ($lonRight - $lonLeft) * $vp_w);
$y = round(($latTop - $lat) / ($latTop - $latBottom) * $vp_h);
$r = 10;
$m = -$r+2;
$l = $r*2-4;
$sites_paths .= <<<SVGSITEPATH
<g
id="site-$index"
transform="
translate($x,$y)">
<circle
id="path83" class="circle"
cx="0" cy="0" r="$r"
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;" />
<path
id="path84"
d="m 0,$m v $l"
style="fill:none;stroke:#ffffff;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1" />
<path
id="path85"
d="m $m,0 h $l"
style="fill:none;stroke:#ffffff;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1" />
<circle
class="site-link"
data-content="$datacontent"
cx="0" cy="0" r="$r"
style="fill:#000000;fill-opacity:0;fill-rule:nonzero;" />
</g>
SVGSITEPATH;
}
$return = [
'#cache' => [
'max-age' => 0,
],
'svg_mapsites' => [
'#theme' => 'svg_mapsites',
'#sites' => $sites_paths,
'#vpw' => $vp_w,
'#vph' => $vp_h,
'#attached' => [
'library' => [
'q2d_mod/sites_map_block',
],
],
]
];
return $return;
// return [
// '#markup' => $this->t('Hello, Sites Map!'),
// ];
}
public function getCacheMaxAge() {
return 0;
}
}