Last active
December 6, 2016 19:34
-
-
Save courtneymyers/b17f63b9df9f52577dc2 to your computer and use it in GitHub Desktop.
Custom function for Drupal 8 theme (functions) file that returns the URL of the next or previous node of a given content type, based on the value of a given date field's timestamp. Returned values can then be saved as variables in a preprocess node function, for use in the content type's Twig template.
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
<?php | |
/** | |
* Custom function to get the URL of the next or previous node of a content type | |
* | |
* Based on: http://mattkorostoff.com/article/simple-next-previous-nav-for-drupal-8 | |
* | |
* @param string $content_type the machine name of a content type to query | |
* @param string $field_name the machine name of the field to query | |
* @param string $field_timestamp time from the date field (YYYY-MM-DD) to query | |
* @param string $link either 'prev' or 'next' | |
* @return string the URL to the next or previous node | |
*/ | |
function getAdjacentNodeURL( $content_type, $field_name, $field_timestamp, $link ) { | |
// setup variables based on $link value | |
if ( $link === 'prev' ) { | |
$opperator = '>'; | |
$sort = 'ASC'; | |
} elseif ( $link === 'next' ) { | |
$opperator = '<'; | |
$sort = 'DESC'; | |
} | |
// query database for one older or one newer node of given content type, based on field timestamp | |
$query = \Drupal::entityQuery('node'); | |
$entity_id = $query->condition('type', $content_type) | |
->condition($field_name. '.value', $field_timestamp, $opperator) | |
->sort($field_name . '.value', $sort) | |
->range(0, 1) | |
->execute(); | |
//kint($entity_id); | |
// if $entity_id isn't the youngest or oldest node in the content type | |
if ( !empty($entity_id) && is_array($entity_id) ) { | |
$node_array = array_values($entity_id); | |
$node_id = $node_array[0]; | |
// get the alias of the node from the built up path | |
$node_url = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $node_id); | |
// return the URL to be stored in variables for theming | |
return $node_url; | |
} | |
} | |
/** | |
* Implements hook_preprocess_HOOK() for node templates. | |
*/ | |
function THEMENAME_preprocess_node( &$variables ) { | |
// Set variables for prev/next links for CONTENTTYPE nodes only | |
if ( $variables['node']->getType() === 'CONTNETTYPE' ) { | |
// get FIELDNAME date: saved in Drupal in MM-DD-YY format (no time info saved) | |
$field_date = $variables['node'] // ContentEntity | |
->get('FIELDNAME') // FieldItemList | |
->get(0) // FieldItem | |
->get('value') // TypedData | |
->getValue(); // string/integer | |
// set variables for theming using custom getAdjacentNodeURL function | |
$variables['prev_node'] = getAdjacentNodeURL( 'CONTENTTYPE', 'FIELDNAME', $field_date, 'prev' ); | |
$variables['next_node'] = getAdjacentNodeURL( 'CONTENTTYPE', 'FIELDNAME', $field_date, 'next' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment