node.rules.inc 4.4 KB

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