node_test.module 4.5 KB

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