|
@@ -0,0 +1,85 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace Drupal\reha_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 'Prevnext' Block.
|
|
|
+ * @Block(
|
|
|
+ * id = "prevnext_block",
|
|
|
+ * admin_label = @Translation("PrevNext Block"),
|
|
|
+ * )
|
|
|
+ */
|
|
|
+class PrevNext extends BlockBase {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * {@inheritdoc}
|
|
|
+ */
|
|
|
+ public function build() {
|
|
|
+ $return = null;
|
|
|
+ /** @var Drupal\node\Entity\Node */
|
|
|
+ $node = \Drupal::routeMatch()->getParameter('node');
|
|
|
+ if ($node) {
|
|
|
+ $nodetype = $node->getType();
|
|
|
+ if($nodetype === "site"){
|
|
|
+ $numerodesite = $node->get('field_numero_site')->getString();
|
|
|
+ $allSites = \Drupal::entityTypeManager()->getStorage('node')
|
|
|
+ ->loadByProperties(['type' => 'site', 'status' => 1, 'field_type_de_site' => 1]);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ usort($allSites, function($a, $b){
|
|
|
+ $numA = $a->get('field_numero_site')->getString();
|
|
|
+ $numB = $b->get('field_numero_site')->getString();
|
|
|
+ if ($numA == $numB) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return ($numA < $numB) ? -1 : 1;
|
|
|
+ });
|
|
|
+ $prevnode = null;
|
|
|
+ $nextnode = null;
|
|
|
+ foreach($allSites as $index => $site){
|
|
|
+ $n = $site->get('field_numero_site')->getString();
|
|
|
+ if($n === $numerodesite){
|
|
|
+ $prevnode = $index - 1 >= 0 ? $allSites[$index - 1] : null;
|
|
|
+ $nextnode = $index + 1 < count($allSites) ? $allSites[$index + 1] : null;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ $return = [
|
|
|
+ '#cache' => [
|
|
|
+ 'max-age' => 0,
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+
|
|
|
+ if ($prevnode) {
|
|
|
+ $prev_link_title = Markup::create('<span>Site pilote précédent</span>');
|
|
|
+ $prev_options = ['absolute' => FALSE, 'attributes' => ['class' => 'prev-site']];
|
|
|
+ $prev_link_object = Link::createFromRoute($prev_link_title, 'entity.node.canonical', ['node' => $prevnode->id()], $prev_options);
|
|
|
+ $return[] = $prev_link_object->toRenderable();
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($nextnode) {
|
|
|
+ $next_link_title = Markup::create('<span>Site pilote suivant</span>');
|
|
|
+ $next_options = ['absolute' => FALSE, 'attributes' => ['class' => 'next-site']];
|
|
|
+ $next_link_object = Link::createFromRoute($next_link_title, 'entity.node.canonical', ['node' => $nextnode->id()], $next_options);
|
|
|
+ $return[] = $next_link_object->toRenderable();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $return;
|
|
|
+ // return [
|
|
|
+ // '#markup' => $this->t('Hello, World!'),
|
|
|
+ // ];
|
|
|
+ }
|
|
|
+ public function getCacheMaxAge() {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|