node.rules.inc 5.2 KB

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