home projets map block

This commit is contained in:
2025-11-10 16:59:31 +01:00
parent 11c4c6d454
commit f73d86d81a
6 changed files with 244 additions and 0 deletions

View File

@@ -0,0 +1,156 @@
<?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;
use Pel\Test\imagetests\CanonEos650dTest;
/**
* Provides a 'Sites Map' Block.
* @Block(
* id = "projetsmap_block",
* admin_label = @Translation("Projets map Block"),
* )
*/
class ProjetsMap extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$return = null;
$allSites = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'site', 'status' => 1]);
$sites_paths = "";
$vp_w = 600;
$vp_h = 600;
// 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){
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
// $title = $site->get('title')->getString();
$title = $site->hasTranslation($language) ? $site->getTranslation($language)->get('title')->getString() : $site->get('title')->getString();
// $subtitle = $site->get('field_sous_titre')->getString();
$subtitle = $site->hasTranslation($language) ? $site->getTranslation($language)->get('field_sous_titre')->getString() : $site->get('field_sous_titre')->getString();
$link_options = ['absolute' => TRUE, 'attributes' => ['class' => 'site-link'], 'language' => \Drupal::languageManager()->getLanguage($langcode)]; // Passer 'absolute' à TRUE
$site_url = $site->toUrl('canonical', $link_options)->toString(); // URL absolue pour le data-url
$site_link_object = Link::createFromRoute($title, 'entity.node.canonical', ['node' => $site->id()], $link_options);
$link = $site_link_object->toString()->getGeneratedLink();
$projets = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties([
'type' => 'projet',
'status' => 1,
'field_site_projet' => $site->id()
]);
$datahtml = "<strong>$link</strong><br>$subtitle<ul>";
foreach($projets as $j => $projet) {
$projet_title = $projet->hasTranslation($language) ? $projet->getTranslation($language)->get('title')->getString() : $projet->get('title')->getString();
$equipe = $projet->hasTranslation($language) ? $projet->getTranslation($language)->get('field_nom_de_l_equipe')->getString() : $projet->get('field_nom_de_l_equipe')->getString();
$laureats = $projet->get('field_laureats')->getString();
// $projet_url = $projet->toUrl('canonical', $link_options)->toString(); // URL absolue pour le data-url
$projet_link_object = Link::createFromRoute($projet_title, 'entity.node.canonical', ['node' => $projet->id()], $link_options);
$projet_link = $projet_link_object->toString()->getGeneratedLink();
$class = $laureats == "1" ? "laureat" : "";
$datahtml .= "<li class='$class'>$projet_link<br/>$equipe</li>";
}
$datahtml .= "</ul>";
$datacontent = htmlspecialchars($datahtml);
$geofield = $site->get('field_geofield')->get(0);
$lon = $geofield->lon;
$lat = $geofield->lat;
$geofieldData = "$lon,$lat"; // Utiliser une chaîne pour stocker la position
$x = round(($lon - $lonLeft) / ($lonRight - $lonLeft) * $vp_w);
$y = round(($latTop - $lat) / ($latTop - $latBottom) * $vp_h);
$r = 12;
$m = -$r/1.5+3;
$l = $r/1.5 +2.5;
$sites_paths .= <<<SVGSITEPATH
<g
id="site-$index"
data-geofield="$geofieldData"
transform="translate($x,$y)"
style="cursor: pointer;" <!-- Change le curseur pour indiquer l'interaction -->
>
<rect width="10" height="10" transform="translate($x,$y)" fill="none" onclick="handleCircleClick('$datacontent')"></rect> <!-- Zone d'interaction invisible -->
<circle
class="site-link"
data-content="$datacontent"
data-url="$site_url"
cx="0" cy="0" r="$r"
style="fill-opacity:1;fill-rule:nonzero;" />
<path
id="path84"
d="m 0,$m v $l"
style="pointer-events: none;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="pointer-events: none;fill:none;stroke:#ffffff;stroke-width:2;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:10;stroke-dasharray:none;stroke-opacity:1" />
</g>
SVGSITEPATH;
}
$return = [
'#cache' => [
'max-age' => 0,
],
'svg_mapsites' => [
'#theme' => 'svg_mapprojets',
'#label' => "Les 30 projets",
'#sites' => $sites_paths,
'#vpw' => $vp_w,
'#vph' => $vp_h,
'#attached' => [
'library' => [
'q2d_mod/projets_map_block',
],
],
]
];
return $return;
// return [
// '#markup' => $this->t('Hello, Sites Map!'),
// ];
}
public function getCacheMaxAge() {
return 0;
}
}