Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function MYMODULE_preprocess_node(&$variables) {
- kpr(Drupal::currentUser()->getLastAccessedTime());
- }
- /**
- * Implements hook_preprocess_HOOK() for node templates.
- */
- function mymodule_preprocess_node(&$variables) {
- $variables['content']['timestamp'] = [
- '#lazy_builder' => ['mymodule_timestamp_current_user', []],
- '#create_placeholder' => TRUE,
- ];
- }
- /**
- * #lazy_builder callback; builds timestamp markup with current user access time.
- */
- function mymodule_timestamp_current_user() {
- $timestamp = Drupal::currentUser()->getLastAccessedTime();
- return [
- '#markup' => $timestamp,
- '#cache' => ['max-age' => 0],
- ];
- }
- use DrupalnodeEntityNodeType;
- use DrupalCoreEntityEntityInterface;
- use DrupalCoreEntityDisplayEntityViewDisplayInterface;
- function MYMODULE_entity_extra_field_info() {
- $extra = [];
- foreach (NodeType::loadMultiple() as $bundle) {
- $extra['node'][$bundle->id()]['display']['last_accessed'] = [
- 'label' => t('Node Last Accessed'),
- 'description' => t('Displays the timestamp the current node got last accessed by the current user'),
- 'visible' => FALSE, // Initially set to hidden on Display Settings.
- ];
- }
- return $extra;
- }
- function MYMODULE_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
- $current_uid = Drupal::currentUser()->id();
- $current_user = DrupaluserEntityUser::load($current_uid);
- $current_nid = $entity->id();
- // Now you have the current user entity and the current node ID.
- // Now you need to get the value of the custom field which probably should
- // be an associated array of node IDs and timestamps.
- $access_data = $current_user->get('field_node_access_data')->getValue();
- // Now you need to look up if the current node ID already is in it and if so,
- // retrieve its timestamp value.
- $timestamp = ... // your job.
- if ($display->getComponent('last_accessed')) {
- $build['last_accessed'] = [
- '#markup' => $timestamp,
- // I hope this works that way. That the cache gets disabled properly for
- // just this pseudo field.
- '#cache' => ['max-age' => 0],
- ];
- }
- // And now finally after the timestamp has been rendered it needs to get updated.
- $current_user->set('field_node_access_data')->... // Your job. Probably something with time().
- $current_user->save();
- }
- /**
- * This creates a database to store the last access timestamp per node per user.
- */
- /**
- * implements HOOK_preprocess_node().
- */
- function node_access_timestamp_by_user_preprocess_node(&$variables) {
- // Set current user ID.
- $uid = Drupal::currentUser()->id();
- // Check if user is authenticated.
- if ($uid != 0) {
- // Get current NID, Timestamp, and DB.
- $currentNID = Drupal::routeMatch()->getRawParameter('node');
- $currentTimestamp = intval(time());
- $database = Drupal::database();
- // Query our DB.
- $queryDBEntries = $database
- ->select('node_access_timestamp_by_user', 'timestamp')
- ->fields('timestamp')
- ->condition('uid', $uid, "=")
- ->condition('nid', $currentNID, "=");
- // Fetch DB data.
- $result = $queryDBEntries->execute()->fetchAll();
- // Check if table row exists and overwrite.
- if ($result[0]->uid == $uid && $result[0]->nid == $currentNID) {
- $database
- ->update('node_access_timestamp_by_user')
- ->fields([
- 'timestamp' => $currentTimestamp,
- ])
- ->condition('uid', $uid, '=')
- ->condition('nid', $currentNID, '=')
- ->execute();
- }
- // Check if table row does not exist and create.
- if ($result[0]->uid != $uid && $result[0]->nid != $currentNID) {
- $database
- ->insert('node_access_timestamp_by_user')
- ->fields([
- 'uid' => $uid,
- 'nid' => $currentNID,
- 'timestamp' => $currentTimestamp,
- ])
- ->execute();
- }
- // Set our $variables to print in twig.
- if ($result[0]->nid == $currentNID) {
- $variables['nodeAccessTimestampByUser_uid'] = $result[0]->uid;
- $variables['nodeAccessTimestampByUser_nid'] = $result[0]->nid;
- $variables['nodeAccessTimestampByUser_timestamp'] = $result[0]->timestamp;
- }
- }
- }
- /**
- * Implements HOOK_install().
- *
- * Creates some default entries on this module custom table.
- *
- * @see hook_install()
- *
- * @ingroup node_access_timestamp_by_user
- */
- function node_access_timestamp_by_user_install() {
- $currentTimestamp = intval(time());
- $database = Drupal::database();
- // Add a default entry.
- $fields = [
- 'uid' => '1',
- 'nid' => '1',
- 'timestamp' => $currentTimestamp,
- ];
- $database
- ->insert('node_access_timestamp_by_user')
- ->fields($fields)
- ->execute();
- }
- /**
- * Implements HOOK_schema().
- *
- * Defines the database tables used by this module.
- *
- * @see hook_schema()
- *
- * @ingroup node_access_timestamp_by_user
- */
- function node_access_timestamp_by_user_schema() {
- $currentTimestamp = intval(time());
- $schema['node_access_timestamp_by_user'] = [
- 'description' => 'Stores node access timestamp by user per node.',
- 'fields' => [
- 'uid' => [
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 1,
- 'description' => "User's {users}.uid",
- ],
- 'nid' => [
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 1,
- 'description' => "Node ID",
- ],
- 'timestamp' => [
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => $currentTimestamp,
- 'description' => "Timestamp",
- ],
- ],
- 'primary key' => ['nid'],
- 'indexes' => [
- 'uid' => ['uid'],
- 'nid' => ['nid'],
- 'timestamp' => ['timestamp'],
- ],
- ];
- return $schema;
- }
- <table>
- <tbody>
- <tr>
- <td>UID</td>
- <td>{{ nodeAccessTimestampByUser_uid }}</td>
- </tr>
- <tr>
- <td>NID</td>
- <td>{{ nodeAccessTimestampByUser_nid }}</td>
- </tr>
- <tr>
- <td>Timestamp</td>
- <td>{{ nodeAccessTimestampByUser_timestamp }}</td>
- </tr>
- </tbody>
- </table>
Add Comment
Please, Sign In to add comment