node.rules.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @file
  4. * Rules integration for the node module.
  5. *
  6. * @addtogroup rules
  7. *
  8. * @{
  9. */
  10. /**
  11. * Implements hook_rules_category_info() on behalf of the node module.
  12. */
  13. function rules_node_category_info() {
  14. return array(
  15. 'node' => array(
  16. 'label' => t('Node'),
  17. 'equals group' => t('Node'),
  18. ),
  19. );
  20. }
  21. /**
  22. * Implements hook_rules_file_info() on behalf of the node module.
  23. */
  24. function rules_node_file_info() {
  25. return array('modules/node.eval');
  26. }
  27. /**
  28. * Implements hook_rules_event_info() on behalf of the node module.
  29. */
  30. function rules_node_event_info() {
  31. $items = array(
  32. 'node_insert' => array(
  33. 'label' => t('After saving new content'),
  34. 'category' => 'node',
  35. 'variables' => rules_events_node_variables(t('created content')),
  36. 'access callback' => 'rules_node_integration_access',
  37. 'class' => 'RulesNodeEventHandler',
  38. ),
  39. 'node_update' => array(
  40. 'label' => t('After updating existing content'),
  41. 'category' => 'node',
  42. 'variables' => rules_events_node_variables(t('updated content'), TRUE),
  43. 'access callback' => 'rules_node_integration_access',
  44. 'class' => 'RulesNodeEventHandler',
  45. ),
  46. 'node_presave' => array(
  47. 'label' => t('Before saving content'),
  48. 'category' => 'node',
  49. 'variables' => rules_events_node_variables(t('saved content'), TRUE),
  50. 'access callback' => 'rules_node_integration_access',
  51. 'class' => 'RulesNodeEventHandler',
  52. ),
  53. 'node_view' => array(
  54. 'label' => t('Content is viewed'),
  55. 'category' => 'node',
  56. 'help' => t("Note that if drupal's page cache is enabled, this event won't be generated for pages served from cache."),
  57. 'variables' => rules_events_node_variables(t('viewed content')) + array(
  58. 'view_mode' => array(
  59. 'type' => 'text',
  60. 'label' => t('view mode'),
  61. 'options list' => 'rules_get_entity_view_modes',
  62. // Add the entity-type for the options list callback.
  63. 'options list entity type' => 'node',
  64. ),
  65. ),
  66. 'access callback' => 'rules_node_integration_access',
  67. 'class' => 'RulesNodeEventHandler',
  68. ),
  69. 'node_delete' => array(
  70. 'label' => t('After deleting content'),
  71. 'category' => 'node',
  72. 'variables' => rules_events_node_variables(t('deleted content')),
  73. 'access callback' => 'rules_node_integration_access',
  74. 'class' => 'RulesNodeEventHandler',
  75. ),
  76. );
  77. // Specify that on presave the node is saved anyway.
  78. $items['node_presave']['variables']['node']['skip save'] = TRUE;
  79. return $items;
  80. }
  81. /**
  82. * Returns some parameter suitable for using it with a node.
  83. */
  84. function rules_events_node_variables($node_label, $update = FALSE) {
  85. $args = array(
  86. 'node' => array('type' => 'node', 'label' => $node_label),
  87. );
  88. if ($update) {
  89. $args += array(
  90. 'node_unchanged' => array(
  91. 'type' => 'node',
  92. 'label' => t('unchanged content'),
  93. 'handler' => 'rules_events_entity_unchanged',
  94. ),
  95. );
  96. }
  97. return $args;
  98. }
  99. /**
  100. * Implements hook_rules_action_info() on behalf of the node module.
  101. */
  102. function rules_node_action_info() {
  103. $defaults = array(
  104. 'parameter' => array(
  105. 'node' => array('type' => 'node', 'label' => t('Content'), 'save' => TRUE),
  106. ),
  107. 'category' => 'node',
  108. 'access callback' => 'rules_node_admin_access',
  109. );
  110. // Add support for hand-picked core actions.
  111. $core_actions = node_action_info();
  112. $actions = array('node_publish_action', 'node_unpublish_action', 'node_make_sticky_action', 'node_make_unsticky_action', 'node_promote_action', 'node_unpromote_action');
  113. foreach ($actions as $base) {
  114. $action_name = str_replace('_action', '', $base);
  115. $items[$action_name] = $defaults + array(
  116. 'label' => $core_actions[$base]['label'],
  117. 'base' => $base,
  118. );
  119. }
  120. return $items;
  121. }
  122. /**
  123. * Node integration access callback.
  124. */
  125. function rules_node_integration_access($type, $name) {
  126. if ($type == 'event' || $type == 'condition') {
  127. return entity_access('view', 'node');
  128. }
  129. }
  130. /**
  131. * Node integration admin access callback.
  132. */
  133. function rules_node_admin_access() {
  134. return user_access('administer nodes');
  135. }
  136. /**
  137. * Event handler support node bundle event settings.
  138. */
  139. class RulesNodeEventHandler extends RulesEventHandlerEntityBundle {
  140. /**
  141. * Returns the label to use for the bundle property.
  142. *
  143. * @return string
  144. */
  145. protected function getBundlePropertyLabel() {
  146. return t('type');
  147. }
  148. }
  149. /**
  150. * @}
  151. */