-
Notifications
You must be signed in to change notification settings - Fork 0
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
AWearring
wants to merge
30
commits into
1.x
Choose a base branch
from
feature/Add-next_previous-navigation
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+551
−1
Open
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 e3dbc1d
Fixing tests
AWearring 32ec383
Added space between chevron and text
AWearring 72278aa
Update tests
AWearring 63497a1
Update tests
AWearring 3a57e9c
Added missing functions in .module
AWearring ba7b13b
Added missing localgov_blogs_theme
AWearring 2102ff1
update to tests
AWearring 592acf8
Update to twig
AWearring a3393fd
Updated Test to use localgov_base theme
AWearring f243093
Solved testing failure?
AWearring 5a648cb
Reworked to use localgov_blog_date to order posts
AWearring eed5f72
Removed redundant code
AWearring e83270c
Coding Standards and Class does not exist
AWearring 9f38793
More coding standards
AWearring 26dd848
Coding standards (last one?)
AWearring f1be3bb
Class naming standards and patch correction
AWearring 5f61cda
Delete src/plugin/block/BlogPrevNextBlock.php
AWearring 85cfd59
Fix missing posts in next prev block
andybroomfield e9bda02
Removed Font-Awesome references
AWearring 8b5bac8
Adding [first attempt at] tests
AWearring 61c63ce
Coding standards on PrevNextBlockTest.php
AWearring 4c72c5b
Coding standards
AWearring 3d27a3a
Add initial Readme
AWearring ee6cb3e
Merge pull request #35 from localgovdrupal/feature/add-documentation
finnlewis 292d26f
Merge branch '1.x' into feature/Add-next_previous-navigation
AWearring bf52288
Test that a localgov blog post next previous links follow channel logic
andybroomfield 5492910
Fix next prev block for blog posts on the same day
andybroomfield a881cb2
Merge branch '1.x' into feature/Add-next_previous-navigation
andybroomfield 96d6a31
fixes typo
markconroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
config/install/field.field.node.localgov_blog_channel.localgov_blog_posts.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
19 changes: 19 additions & 0 deletions
19
config/optional/block.block.localgov_blogs_prev_next_block_base.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
->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 ''; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.