Guest User

Untitled

a guest
Dec 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.95 KB | None | 0 0
  1. function MYMODULE_preprocess_node(&$variables) {
  2. kpr(Drupal::currentUser()->getLastAccessedTime());
  3. }
  4.  
  5. /**
  6. * Implements hook_preprocess_HOOK() for node templates.
  7. */
  8. function mymodule_preprocess_node(&$variables) {
  9. $variables['content']['timestamp'] = [
  10. '#lazy_builder' => ['mymodule_timestamp_current_user', []],
  11. '#create_placeholder' => TRUE,
  12. ];
  13. }
  14.  
  15. /**
  16. * #lazy_builder callback; builds timestamp markup with current user access time.
  17. */
  18. function mymodule_timestamp_current_user() {
  19. $timestamp = Drupal::currentUser()->getLastAccessedTime();
  20. return [
  21. '#markup' => $timestamp,
  22. '#cache' => ['max-age' => 0],
  23. ];
  24. }
  25.  
  26. use DrupalnodeEntityNodeType;
  27. use DrupalCoreEntityEntityInterface;
  28. use DrupalCoreEntityDisplayEntityViewDisplayInterface;
  29.  
  30. function MYMODULE_entity_extra_field_info() {
  31.  
  32. $extra = [];
  33.  
  34. foreach (NodeType::loadMultiple() as $bundle) {
  35.  
  36. $extra['node'][$bundle->id()]['display']['last_accessed'] = [
  37. 'label' => t('Node Last Accessed'),
  38. 'description' => t('Displays the timestamp the current node got last accessed by the current user'),
  39. 'visible' => FALSE, // Initially set to hidden on Display Settings.
  40. ];
  41. }
  42.  
  43. return $extra;
  44. }
  45.  
  46. function MYMODULE_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
  47.  
  48. $current_uid = Drupal::currentUser()->id();
  49. $current_user = DrupaluserEntityUser::load($current_uid);
  50.  
  51. $current_nid = $entity->id();
  52.  
  53. // Now you have the current user entity and the current node ID.
  54.  
  55. // Now you need to get the value of the custom field which probably should
  56. // be an associated array of node IDs and timestamps.
  57. $access_data = $current_user->get('field_node_access_data')->getValue();
  58.  
  59. // Now you need to look up if the current node ID already is in it and if so,
  60. // retrieve its timestamp value.
  61. $timestamp = ... // your job.
  62.  
  63. if ($display->getComponent('last_accessed')) {
  64.  
  65. $build['last_accessed'] = [
  66. '#markup' => $timestamp,
  67. // I hope this works that way. That the cache gets disabled properly for
  68. // just this pseudo field.
  69. '#cache' => ['max-age' => 0],
  70. ];
  71. }
  72.  
  73. // And now finally after the timestamp has been rendered it needs to get updated.
  74. $current_user->set('field_node_access_data')->... // Your job. Probably something with time().
  75. $current_user->save();
  76. }
  77.  
  78. /**
  79. * This creates a database to store the last access timestamp per node per user.
  80. */
  81.  
  82. /**
  83. * implements HOOK_preprocess_node().
  84. */
  85. function node_access_timestamp_by_user_preprocess_node(&$variables) {
  86.  
  87. // Set current user ID.
  88. $uid = Drupal::currentUser()->id();
  89.  
  90. // Check if user is authenticated.
  91. if ($uid != 0) {
  92.  
  93. // Get current NID, Timestamp, and DB.
  94. $currentNID = Drupal::routeMatch()->getRawParameter('node');
  95. $currentTimestamp = intval(time());
  96. $database = Drupal::database();
  97.  
  98. // Query our DB.
  99. $queryDBEntries = $database
  100. ->select('node_access_timestamp_by_user', 'timestamp')
  101. ->fields('timestamp')
  102. ->condition('uid', $uid, "=")
  103. ->condition('nid', $currentNID, "=");
  104.  
  105. // Fetch DB data.
  106. $result = $queryDBEntries->execute()->fetchAll();
  107.  
  108. // Check if table row exists and overwrite.
  109. if ($result[0]->uid == $uid && $result[0]->nid == $currentNID) {
  110. $database
  111. ->update('node_access_timestamp_by_user')
  112. ->fields([
  113. 'timestamp' => $currentTimestamp,
  114. ])
  115. ->condition('uid', $uid, '=')
  116. ->condition('nid', $currentNID, '=')
  117. ->execute();
  118. }
  119.  
  120. // Check if table row does not exist and create.
  121. if ($result[0]->uid != $uid && $result[0]->nid != $currentNID) {
  122. $database
  123. ->insert('node_access_timestamp_by_user')
  124. ->fields([
  125. 'uid' => $uid,
  126. 'nid' => $currentNID,
  127. 'timestamp' => $currentTimestamp,
  128. ])
  129. ->execute();
  130. }
  131.  
  132. // Set our $variables to print in twig.
  133. if ($result[0]->nid == $currentNID) {
  134. $variables['nodeAccessTimestampByUser_uid'] = $result[0]->uid;
  135. $variables['nodeAccessTimestampByUser_nid'] = $result[0]->nid;
  136. $variables['nodeAccessTimestampByUser_timestamp'] = $result[0]->timestamp;
  137. }
  138.  
  139. }
  140.  
  141. }
  142.  
  143. /**
  144. * Implements HOOK_install().
  145. *
  146. * Creates some default entries on this module custom table.
  147. *
  148. * @see hook_install()
  149. *
  150. * @ingroup node_access_timestamp_by_user
  151. */
  152. function node_access_timestamp_by_user_install() {
  153.  
  154. $currentTimestamp = intval(time());
  155. $database = Drupal::database();
  156.  
  157. // Add a default entry.
  158. $fields = [
  159. 'uid' => '1',
  160. 'nid' => '1',
  161. 'timestamp' => $currentTimestamp,
  162. ];
  163. $database
  164. ->insert('node_access_timestamp_by_user')
  165. ->fields($fields)
  166. ->execute();
  167. }
  168.  
  169. /**
  170. * Implements HOOK_schema().
  171. *
  172. * Defines the database tables used by this module.
  173. *
  174. * @see hook_schema()
  175. *
  176. * @ingroup node_access_timestamp_by_user
  177. */
  178. function node_access_timestamp_by_user_schema() {
  179.  
  180. $currentTimestamp = intval(time());
  181.  
  182. $schema['node_access_timestamp_by_user'] = [
  183. 'description' => 'Stores node access timestamp by user per node.',
  184. 'fields' => [
  185. 'uid' => [
  186. 'type' => 'int',
  187. 'not null' => TRUE,
  188. 'default' => 1,
  189. 'description' => "User's {users}.uid",
  190. ],
  191. 'nid' => [
  192. 'type' => 'int',
  193. 'not null' => TRUE,
  194. 'default' => 1,
  195. 'description' => "Node ID",
  196. ],
  197. 'timestamp' => [
  198. 'type' => 'int',
  199. 'not null' => TRUE,
  200. 'default' => $currentTimestamp,
  201. 'description' => "Timestamp",
  202. ],
  203. ],
  204. 'primary key' => ['nid'],
  205. 'indexes' => [
  206. 'uid' => ['uid'],
  207. 'nid' => ['nid'],
  208. 'timestamp' => ['timestamp'],
  209. ],
  210. ];
  211.  
  212. return $schema;
  213. }
  214.  
  215. <table>
  216. <tbody>
  217. <tr>
  218. <td>UID</td>
  219. <td>{{ nodeAccessTimestampByUser_uid }}</td>
  220. </tr>
  221. <tr>
  222. <td>NID</td>
  223. <td>{{ nodeAccessTimestampByUser_nid }}</td>
  224. </tr>
  225. <tr>
  226. <td>Timestamp</td>
  227. <td>{{ nodeAccessTimestampByUser_timestamp }}</td>
  228. </tr>
  229. </tbody>
  230. </table>
Add Comment
Please, Sign In to add comment