2015-08-17 17:00:26 -07:00
< ? php
/**
* @ file
* User page callbacks for tracker . module .
*/
use Drupal\Core\Cache\Cache ;
use Drupal\node\Entity\Node ;
/**
* Page callback : Generates a page of tracked nodes for the site .
*
* Queries the database for info , adds RDFa info if applicable , and generates
* the render array that will be used to render the page .
*
2016-01-06 16:31:26 -08:00
* @ param \Drupal\user\UserInterface $account
* ( optional ) The user account to track .
*
2015-08-17 17:00:26 -07:00
* @ return array
* A renderable array .
*
* @ see tracker_menu ()
*/
function tracker_page ( $account = NULL ) {
if ( $account ) {
$query = db_select ( 'tracker_user' , 't' )
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
-> addMetaData ( 'base_table' , 'tracker_user' )
-> condition ( 't.uid' , $account -> id ());
}
else {
2017-04-13 15:53:35 +01:00
$query = db_select ( 'tracker_node' , 't' , [ 'target' => 'replica' ])
2015-08-17 17:00:26 -07:00
-> extend ( 'Drupal\Core\Database\Query\PagerSelectExtender' )
-> addMetaData ( 'base_table' , 'tracker_node' );
}
// This array acts as a placeholder for the data selected later
// while keeping the correct order.
$tracker_data = $query
-> addTag ( 'node_access' )
2017-04-13 15:53:35 +01:00
-> fields ( 't' , [ 'nid' , 'changed' ])
2015-08-17 17:00:26 -07:00
-> condition ( 't.published' , 1 )
-> orderBy ( 't.changed' , 'DESC' )
-> limit ( 25 )
-> execute ()
-> fetchAllAssoc ( 'nid' );
$cache_tags = [];
$rows = [];
if ( ! empty ( $tracker_data )) {
// Load nodes into an array with the same order as $tracker_data.
$nodes = Node :: loadMultiple ( array_keys ( $tracker_data ));
// Enrich the node data.
$result = \Drupal :: service ( 'comment.statistics' ) -> read ( $nodes , 'node' , FALSE );
foreach ( $result as $statistics ) {
// The node ID may not be unique; there can be multiple comment fields.
// Make comment_count the total of all comments.
$nid = $statistics -> entity_id ;
if ( empty ( $nodes [ $nid ] -> comment_count )
|| ! is_numeric ( $nodes [ $nid ] -> comment_count )) {
$nodes [ $nid ] -> comment_count = $statistics -> comment_count ;
}
else {
$nodes [ $nid ] -> comment_count += $statistics -> comment_count ;
}
2015-08-27 12:03:05 -07:00
// Make the last comment timestamp reflect the latest comment.
if ( ! isset ( $nodes [ $nid ] -> last_comment_timestamp )) {
$nodes [ $nid ] -> last_comment_timestamp = $statistics -> last_comment_timestamp ;
}
else {
$nodes [ $nid ] -> last_comment_timestamp = max ( $nodes [ $nid ] -> last_comment_timestamp , $statistics -> last_comment_timestamp );
}
2015-08-17 17:00:26 -07:00
}
// Display the data.
foreach ( $nodes as $node ) {
// Set the last activity time from tracker data. This also takes into
// account comment activity, so getChangedTime() is not used.
$node -> last_activity = $tracker_data [ $node -> id ()] -> changed ;
// Determine the number of comments.
$comments = 0 ;
if ( $node -> comment_count ) {
$comments = $node -> comment_count ;
}
2017-04-13 15:53:35 +01:00
$row = [
2015-09-04 13:20:09 -07:00
'type' => node_get_type_label ( $node ),
2017-04-13 15:53:35 +01:00
'title' => [
'data' => [
2015-08-17 17:00:26 -07:00
'#type' => 'link' ,
'#url' => $node -> urlInfo (),
'#title' => $node -> getTitle (),
2017-04-13 15:53:35 +01:00
],
2015-08-27 12:03:05 -07:00
'data-history-node-id' => $node -> id (),
'data-history-node-timestamp' => $node -> getChangedTime (),
2017-04-13 15:53:35 +01:00
],
'author' => [
'data' => [
2015-08-17 17:00:26 -07:00
'#theme' => 'username' ,
'#account' => $node -> getOwner (),
2017-04-13 15:53:35 +01:00
],
],
'comments' => [
'class' => [ 'comments' ],
2015-08-17 17:00:26 -07:00
'data' => $comments ,
2015-08-27 12:03:05 -07:00
'data-history-node-last-comment-timestamp' => $node -> last_comment_timestamp ,
2017-04-13 15:53:35 +01:00
],
'last updated' => [
'data' => t ( '@time ago' , [
2015-10-08 11:40:12 -07:00
'@time' => \Drupal :: service ( 'date.formatter' ) -> formatTimeDiffSince ( $node -> last_activity ),
2017-04-13 15:53:35 +01:00
]),
],
];
2015-08-17 17:00:26 -07:00
$rows [] = $row ;
// Add node and node owner to cache tags.
$cache_tags = Cache :: mergeTags ( $cache_tags , $node -> getCacheTags ());
if ( $node -> getOwner ()) {
$cache_tags = Cache :: mergeTags ( $cache_tags , $node -> getOwner () -> getCacheTags ());
}
}
}
// Add the list cache tag for nodes.
$cache_tags = Cache :: mergeTags ( $cache_tags , \Drupal :: entityManager () -> getDefinition ( 'node' ) -> getListCacheTags ());
2017-04-13 15:53:35 +01:00
$page [ 'tracker' ] = [
2015-08-17 17:00:26 -07:00
'#rows' => $rows ,
2017-04-13 15:53:35 +01:00
'#header' => [ t ( 'Type' ), t ( 'Title' ), t ( 'Author' ), t ( 'Comments' ), t ( 'Last updated' )],
2015-08-17 17:00:26 -07:00
'#type' => 'table' ,
'#empty' => t ( 'No content available.' ),
2017-04-13 15:53:35 +01:00
];
$page [ 'pager' ] = [
2015-08-17 17:00:26 -07:00
'#type' => 'pager' ,
'#weight' => 10 ,
2017-04-13 15:53:35 +01:00
];
2015-08-17 17:00:26 -07:00
$page [ '#sorted' ] = TRUE ;
$page [ '#cache' ][ 'tags' ] = $cache_tags ;
$page [ '#cache' ][ 'contexts' ][] = 'user.node_grants:view' ;
2015-09-04 13:20:09 -07:00
// Display the reading history if that module is enabled.
if ( \Drupal :: moduleHandler () -> moduleExists ( 'history' )) {
// Reading history is tracked for authenticated users only.
if ( \Drupal :: currentUser () -> isAuthenticated ()) {
$page [ '#attached' ][ 'library' ][] = 'tracker/history' ;
}
$page [ '#cache' ][ 'contexts' ][] = 'user.roles:authenticated' ;
2015-08-27 12:03:05 -07:00
}
2015-08-17 17:00:26 -07:00
return $page ;
}