node_test.module 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. /**
  3. * @file
  4. * A dummy module for testing node related hooks.
  5. *
  6. * This is a dummy module that implements node related hooks to test API
  7. * interaction with the Node module.
  8. */
  9. /**
  10. * Implements hook_node_load().
  11. */
  12. function node_test_node_load($nodes, $types) {
  13. // Add properties to each loaded node which record the parameters that were
  14. // passed in to this function, so the tests can check that (a) this hook was
  15. // called, and (b) the parameters were what we expected them to be.
  16. $nids = array_keys($nodes);
  17. ksort($nids);
  18. sort($types);
  19. foreach ($nodes as $node) {
  20. $node->node_test_loaded_nids = $nids;
  21. $node->node_test_loaded_types = $types;
  22. }
  23. }
  24. /**
  25. * Implements hook_node_view().
  26. */
  27. function node_test_node_view($node, $view_mode) {
  28. if ($view_mode == 'rss') {
  29. // Add RSS elements and namespaces when building the RSS feed.
  30. $node->rss_elements[] = array(
  31. 'key' => 'testElement',
  32. 'value' => t('Value of testElement RSS element for node !nid.', array('!nid' => $node->nid)),
  33. );
  34. $node->rss_namespaces['xmlns:drupaltest'] = 'http://example.com/test-namespace';
  35. // Add content that should be displayed only in the RSS feed.
  36. $node->content['extra_feed_content'] = array(
  37. '#markup' => '<p>' . t('Extra data that should appear only in the RSS feed for node !nid.', array('!nid' => $node->nid)) . '</p>',
  38. '#weight' => 10,
  39. );
  40. }
  41. if ($view_mode != 'rss') {
  42. // Add content that should NOT be displayed in the RSS feed.
  43. $node->content['extra_non_feed_content'] = array(
  44. '#markup' => '<p>' . t('Extra data that should appear everywhere except the RSS feed for node !nid.', array('!nid' => $node->nid)) . '</p>',
  45. );
  46. }
  47. }
  48. /**
  49. * Implements hook_node_grants().
  50. */
  51. function node_test_node_grants($account, $op) {
  52. // Give everyone full grants so we don't break other node tests.
  53. // Our node access tests asserts three realms of access.
  54. // See testGrantAlter().
  55. return array(
  56. 'test_article_realm' => array(1),
  57. 'test_page_realm' => array(1),
  58. 'test_alter_realm' => array(2),
  59. );
  60. }
  61. /**
  62. * Implements hook_node_access_records().
  63. */
  64. function node_test_node_access_records($node) {
  65. // Return nothing when testing for empty responses.
  66. if (!empty($node->disable_node_access)) {
  67. return;
  68. }
  69. $grants = array();
  70. if ($node->type == 'article') {
  71. // Create grant in arbitrary article_realm for article nodes.
  72. $grants[] = array(
  73. 'realm' => 'test_article_realm',
  74. 'gid' => 1,
  75. 'grant_view' => 1,
  76. 'grant_update' => 0,
  77. 'grant_delete' => 0,
  78. 'priority' => 0,
  79. );
  80. }
  81. elseif ($node->type == 'page') {
  82. // Create grant in arbitrary page_realm for page nodes.
  83. $grants[] = array(
  84. 'realm' => 'test_page_realm',
  85. 'gid' => 1,
  86. 'grant_view' => 1,
  87. 'grant_update' => 0,
  88. 'grant_delete' => 0,
  89. 'priority' => 0,
  90. );
  91. }
  92. return $grants;
  93. }
  94. /**
  95. * Implements hook_node_access_records_alter().
  96. */
  97. function node_test_node_access_records_alter(&$grants, $node) {
  98. if (!empty($grants)) {
  99. foreach ($grants as $key => $grant) {
  100. // Alter grant from test_page_realm to test_alter_realm and modify the gid.
  101. if ($grant['realm'] == 'test_page_realm' && $node->promote) {
  102. $grants[$key]['realm'] = 'test_alter_realm';
  103. $grants[$key]['gid'] = 2;
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Implements hook_node_grants_alter().
  110. */
  111. function node_test_node_grants_alter(&$grants, $account, $op) {
  112. // Return an empty array of grants to prove that we can alter by reference.
  113. $grants = array();
  114. }
  115. /**
  116. * Implements hook_node_presave().
  117. */
  118. function node_test_node_presave($node) {
  119. if ($node->title == 'testing_node_presave') {
  120. // Sun, 19 Nov 1978 05:00:00 GMT
  121. $node->created = 280299600;
  122. // Drupal 1.0 release.
  123. $node->changed = 979534800;
  124. }
  125. // Determine changes.
  126. if (!empty($node->original) && $node->original->title == 'test_changes') {
  127. if ($node->original->title != $node->title) {
  128. $node->title .= '_presave';
  129. }
  130. }
  131. }
  132. /**
  133. * Implements hook_node_update().
  134. */
  135. function node_test_node_update($node) {
  136. // Determine changes on update.
  137. if (!empty($node->original) && $node->original->title == 'test_changes') {
  138. if ($node->original->title != $node->title) {
  139. $node->title .= '_update';
  140. }
  141. }
  142. }
  143. /**
  144. * Implements hook_entity_view_mode_alter().
  145. */
  146. function node_test_entity_view_mode_alter(&$view_mode, $context) {
  147. // Only alter the view mode if we are on the test callback.
  148. if ($change_view_mode = variable_get('node_test_change_view_mode', '')) {
  149. $view_mode = $change_view_mode;
  150. }
  151. }
  152. /**
  153. * Implements hook_node_insert().
  154. *
  155. * This tests saving a node on node insert.
  156. *
  157. * @see NodeSaveTest::testNodeSaveOnInsert()
  158. */
  159. function node_test_node_insert($node) {
  160. // Set the node title to the node ID and save.
  161. if ($node->title == 'new') {
  162. $node->title = 'Node '. $node->nid;
  163. // Remove the is_new flag, so that the node is updated and not inserted
  164. // again.
  165. unset($node->is_new);
  166. node_save($node);
  167. }
  168. }