prev next actu

This commit is contained in:
Bachir Soussi Chiadmi 2024-07-08 16:06:59 +02:00
parent a275d1f0d4
commit d636cb8d4d
6 changed files with 129 additions and 1 deletions

View File

@ -0,0 +1,28 @@
uuid: 67f1896c-d7c8-4128-8dcb-aefbd30ec51c
langcode: fr
status: true
dependencies:
module:
- node
- q2d_mod
theme:
- quartiers_de_demain
id: quartiers_de_demain_prevnextblock
theme: quartiers_de_demain
region: content
weight: -6
provider: null
plugin: prevnext_block
settings:
id: prevnext_block
label: 'PrevNext Block'
label_display: '0'
provider: q2d_mod
visibility:
'entity_bundle:node':
id: 'entity_bundle:node'
negate: false
context_mapping:
node: '@node.node_route_context:node'
bundles:
actualite: actualite

View File

@ -7,7 +7,7 @@ dependencies:
id: quartiers_de_demain_titredepage id: quartiers_de_demain_titredepage
theme: quartiers_de_demain theme: quartiers_de_demain
region: content region: content
weight: 0 weight: -5
provider: null provider: null
plugin: page_title_block plugin: page_title_block
settings: settings:

View File

@ -90,6 +90,7 @@ module:
path_alias_xt: 0 path_alias_xt: 0
pathologic: 0 pathologic: 0
persistent_login: 0 persistent_login: 0
q2d_mod: 0
redis: 0 redis: 0
responsive_image: 0 responsive_image: 0
search_api: 0 search_api: 0

View File

@ -0,0 +1,7 @@
name: q2d_mod
type: module
description: Provides additional functionality for the site.
package: Custom
core_version_requirement: ^9 || ^10
dependencies:
- drupal:block

View File

@ -0,0 +1,7 @@
<?php
/**
* @file
* Primary module hooks for reha module.
*/

View File

@ -0,0 +1,85 @@
<?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 '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 === "actualite"){
$date = $node->get('field_date')->getString();
$allActus = \Drupal::entityTypeManager()->getStorage('node')
->loadByProperties(['type' => 'actualite', 'status' => 1]);
usort($allActus, function($a, $b){
$dateA = $a->get('field_date')->getString();
$dateB = $b->get('field_date')->getString();
if ($dateA == $dateB) {
return 0;
}
return ($dateA < $dateB) ? -1 : 1;
});
$prevnode = null;
$nextnode = null;
foreach($allActus as $index => $actu){
$n = $actu->get('field_date')->getString();
if($n === $date){
$prevnode = $index - 1 >= 0 ? $allActus[$index - 1] : null;
$nextnode = $index + 1 < count($allActus) ? $allActus[$index + 1] : null;
break;
}
}
$return = [
'#cache' => [
'max-age' => 0,
]
];
if (isset($prevnode)) {
$prev_link_title = Markup::create('<span>Précédent</span>');
$prev_options = ['absolute' => FALSE, 'attributes' => ['class' => 'prev-actu']];
$prev_link_object = Link::createFromRoute($prev_link_title, 'entity.node.canonical', ['node' => $prevnode->id()], $prev_options);
$return[] = $prev_link_object->toRenderable();
}
if (isset($nextnode)) {
$next_link_title = Markup::create('<span>Suivant</span>');
$next_options = ['absolute' => FALSE, 'attributes' => ['class' => 'next-actu']];
$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;
}
}