Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add next/previous navigation #31

Open
wants to merge 30 commits into
base: 1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ccfa5cb
Add next/previous navigation
AWearring Mar 21, 2024
e3dbc1d
Fixing tests
AWearring Mar 21, 2024
32ec383
Added space between chevron and text
AWearring Mar 21, 2024
72278aa
Update tests
AWearring Mar 21, 2024
63497a1
Update tests
AWearring Mar 21, 2024
3a57e9c
Added missing functions in .module
AWearring Mar 23, 2024
ba7b13b
Added missing localgov_blogs_theme
AWearring Mar 23, 2024
2102ff1
update to tests
AWearring Mar 23, 2024
592acf8
Update to twig
AWearring Mar 23, 2024
a3393fd
Updated Test to use localgov_base theme
AWearring Mar 26, 2024
f243093
Solved testing failure?
AWearring Mar 26, 2024
5a648cb
Reworked to use localgov_blog_date to order posts
AWearring Apr 19, 2024
eed5f72
Removed redundant code
AWearring Apr 30, 2024
e83270c
Coding Standards and Class does not exist
AWearring Apr 30, 2024
9f38793
More coding standards
AWearring Apr 30, 2024
26dd848
Coding standards (last one?)
AWearring Apr 30, 2024
f1be3bb
Class naming standards and patch correction
AWearring May 21, 2024
5f61cda
Delete src/plugin/block/BlogPrevNextBlock.php
AWearring May 21, 2024
85cfd59
Fix missing posts in next prev block
andybroomfield May 22, 2024
e9bda02
Removed Font-Awesome references
AWearring May 22, 2024
8b5bac8
Adding [first attempt at] tests
AWearring May 24, 2024
61c63ce
Coding standards on PrevNextBlockTest.php
AWearring May 24, 2024
4c72c5b
Coding standards
AWearring May 24, 2024
3d27a3a
Add initial Readme
AWearring May 24, 2024
ee6cb3e
Merge pull request #35 from localgovdrupal/feature/add-documentation
finnlewis May 28, 2024
292d26f
Merge branch '1.x' into feature/Add-next_previous-navigation
AWearring May 28, 2024
bf52288
Test that a localgov blog post next previous links follow channel logic
andybroomfield Jul 2, 2024
5492910
Fix next prev block for blog posts on the same day
andybroomfield Jul 2, 2024
a881cb2
Merge branch '1.x' into feature/Add-next_previous-navigation
andybroomfield Jul 2, 2024
96d6a31
fixes typo
markconroy Aug 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
langcode: en
status: true
dependencies:
config:
- field.storage.node.localgov_blog_posts
- node.type.localgov_blog_channel
- node.type.localgov_blog_post
id: node.localgov_blog_channel.localgov_blog_posts
field_name: localgov_blog_posts
entity_type: node
bundle: localgov_blog_channel
label: localgov_blog_posts
description: ''
required: false
translatable: false
default_value: { }
default_value_callback: ''
settings:
handler: 'default:node'
handler_settings:
target_bundles:
localgov_blog_post: localgov_blog_post
sort:
field: _none
direction: ASC
auto_create: false
auto_create_bundle: ''
field_type: entity_reference
18 changes: 18 additions & 0 deletions config/install/field.storage.node.localgov_blog_posts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
langcode: en
status: true
dependencies:
module:
- node
id: node.localgov_blog_posts
field_name: localgov_blog_posts
entity_type: node
type: entity_reference
settings:
target_type: node
module: core
locked: false
cardinality: -1
translatable: true
indexes: { }
persist_with_no_fields: false
custom_storage: false
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
langcode: en
status: true
dependencies:
module:
- localgov_blogs
theme:
- localgov_base
id: localgov_blogs_prev_next_block_base
theme: localgov_base
region: content_bottom
weight: -6
provider: null
plugin: localgov_blogs_prev_next_block
settings:
id: localgov_blogs_prev_next_block
label: 'Blogs prev next block'
provider: localgov_blogs
label_display: '0'
visibility: { }
7 changes: 7 additions & 0 deletions config/schema/localgov_blogs_prev_next_block_base.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
block.settings.localgov_blogs_prev_next_block:
type: block_settings
label: 'Previous Next button block settings'
mapping:
show_title:
type: boolean
label: 'Show title'
17 changes: 17 additions & 0 deletions localgov_blogs.module
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ use Drupal\node\NodeForm;
use Drupal\node\NodeInterface;
use Drupal\views\Views;

/**
* Implements hook_theme().
*/
function localgov_blogs_theme($existing, $type, $theme, $path) {
return [
'localgov_blogs_prev_next_block' => [
'variables' => [
'previous_url' => NULL,
'previous_title' => NULL,
'next_url' => NULL,
'next_title' => NULL,
'show_title' => NULL,
],
],
];
}

/**
* Implements hook_entity_extra_field_info().
*/
Expand Down
233 changes: 233 additions & 0 deletions src/Plugin/Block/BlogPrevNextBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php

namespace Drupal\localgov_blogs\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Provides a 'Next Previous' block.
*
* @Block(
* id = "localgov_blogs_prev_next_block",
* admin_label = @Translation("Blog Previous Next block"),
* category = @Translation("Blocks")
* )
*/
class BlogPrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The current route match.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* Node being displayed.
*
* @var \Drupal\node\NodeInterface
*/
protected $node;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* {@inheritdoc}
*/
public static function create(
ContainerInterface $container,
array $configuration,
$plugin_id,
$plugin_definition,
) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('current_route_match'),
$container->get('entity_type.manager')
);
}

/**
* Creates a PrevNextBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The current route match.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
RouteMatchInterface $route_match,
EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->entityTypeManager = $entityTypeManager;
if ($this->routeMatch->getParameter('node')) {
$this->node = $this->routeMatch->getParameter('node');
if (!$this->node instanceof NodeInterface) {
$node_storage = $this->entityTypeManager->getStorage('node');
$this->node = $node_storage->load($this->node);
}
}
}

/**
* {@inheritdoc}
*/
public function build() {

$node = $this->routeMatch->getParameter('node');
$previous_url = '';
$previous_title = '';
$next_url = '';
$next_title = '';

if ($node instanceof NodeInterface && $node->getType() == 'localgov_blog_post') {

$prev = $this->generatePrevious($node);
if (!empty($prev)) {
$previous_title = $prev['title'];
$previous_url = $prev['url'];
}

$next = $this->generateNext($node);
if (!empty($next)) {
$next_title = $next['title'];
$next_url = $next['url'];
}
}

return [
'#theme' => 'localgov_blogs_prev_next_block',
'#previous_url' => $previous_url,
'#previous_title' => $previous_title,
'#next_url' => $next_url,
'#next_title' => $next_title,
];
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
// Get the created time of the current node.
$node = $this->routeMatch->getParameter('node');
if (!empty($node) && $node instanceof NodeInterface) {
// If there is node add its cachetag.
return Cache::mergeTags(parent::getCacheTags(), ['node:' . $node->id()]);
}
else {
// Return default tags instead.
return parent::getCacheTags();
}
}

/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['route']);
}

/**
* Lookup the previous node,youngest node which is still older than the node.
*
* @param string $node
* Show current page node id.
*
* @return array
* A render array for a previous node.
*/
private function generatePrevious($node) {
return $this->generateNextPrevious($node, 'prev');
}

/**
* Lookup the next node,oldest node which is still younger than the node.
*
* @param string $node
* Show current page node id.
*
* @return array
* A render array for a next node.
*/
private function generateNext($node) {
return $this->generateNextPrevious($node, 'next');
}

const DIRECTION__NEXT = 'next';

/**
* Lookup the next or previous node.
*
* @param string $node
* Get current page node id.
* @param string $direction
* Default value is "next" and other value come from
* generatePrevious() and generatePrevious().
*
* @return array
* Find the alias of the next node.
*/
private function generateNextPrevious($node, $direction = self::DIRECTION__NEXT) {
$comparison_opperator = '>';
$sort = 'ASC';
$current_node_date = $node->get('localgov_blog_date')->value;
$current_langcode = $node->get('langcode')->value;
$current_blog_channel = $node->get('localgov_blog_channel')->target_id;

if ($direction === 'prev') {
$comparison_opperator = '<';
$sort = 'DESC';
}

// Lookup 1 node younger (or older) than the current node
// based upon the `localgov_blog_date` field.
$query = $this->entityTypeManager->getStorage('node');
$query_result = $query->getQuery();
$result = $query_result->condition('localgov_blog_date', $current_node_date, $comparison_opperator)
->condition('type', 'localgov_blog_post')
->condition('localgov_blog_channel', $current_blog_channel)
->condition('status', 1)
->condition('langcode', $current_langcode)
->sort('localgov_blog_date', $sort)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made two posts on the same day. They don't get any previous next links. I think this is because the granularity of the date in day. If you have two posts on the same day it's not getting to them.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekes I think this is becuase the blog date field is a date, not a date time. Simmilar issue in news as it's not got time granuallity. I suggest for this PR we add in checking for a page updated also, and then revise these date fields on blogs and news and convert them to datetime.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekes @andybroomfield If eiether of you have some time to look at adding in a check for page updated that would be awesome - I have very limited time at the moment to look at this so any help would be greatly appreciated.

->range(0, 1)
->accessCheck(TRUE)
->execute();

// If this is not the youngest (or oldest) node.
if (!empty($result) && is_array($result)) {
$result = array_values($result)[0];
$node = $query->load($result);

return [
'title' => $node->get('title')->value,
'url' => $node->toUrl()->toString(),
];
}
return '';
}

}
25 changes: 25 additions & 0 deletions templates/localgov-blogs-prev-next-block.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{#
/**
* @file
* Default theme implementation for the localgov_blogs_prev_next_block block.
*/
#}
<nav class="localgov-blog-navigation">
{% if previous_url %}
<a href="{{ previous_url }}" class="localgov-blog-navigation__previous" rel="prev" aria-label="{{ 'Previous'|t }}: {{ previous_title }}">
<span class="localgov-blog-navigation__navigation-wrapper">
<span class="localgov-blog-navigation__navigation-nextprev">{{ 'Previous'|t }}<span class="visually-hidden">: </span></span>
<span class="localgov-blog-navigation__navigation-title">{{ previous_title }}</span>
</span>
</a>
{% endif %}

{% if next_url %}
<a href="{{ next_url }}" class="localgov-blog-navigation__next" rel="next" aria-label="{{ 'Next'|t }}: {{ next_title }}">
<span class="localgov-blog-navigation__navigation-wrapper">
<span class="localgov-blog-navigation__navigation-nextprev">{{ 'Next'|t }}<span class="visually-hidden">: </span></span>
<span class=" localgov-blog-navigation__navigation-title">{{ next_title }}</span>
</span>
</a>
{% endif %}
</nav>
Loading
Loading