entity.rules.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * Provides Rules integration for entities provided via the CRUD API.
  5. *
  6. * Rules automatically provides us with actions for CRUD and a suiting entity
  7. * data type. For events the controller automatically invokes Rules events once
  8. * Rules is active, so we just have to provide the appropriate info.
  9. */
  10. /**
  11. * Default controller for generating Rules integration.
  12. */
  13. class EntityDefaultRulesController {
  14. protected $type, $info;
  15. public function __construct($type) {
  16. $this->type = $type;
  17. $this->info = entity_get_info($type);
  18. }
  19. public function eventInfo() {
  20. $info = $this->info;
  21. $type = $this->type;
  22. $label = $info['label'];
  23. $defaults = array(
  24. 'module' => isset($info['module']) ? $info['module'] : 'entity',
  25. 'group' => $label,
  26. 'access callback' => 'entity_rules_integration_event_access',
  27. );
  28. $items[$type . '_insert'] = $defaults + array(
  29. 'label' => t('After saving a new @entity', array('@entity' => drupal_strtolower($label))),
  30. 'variables' => entity_rules_events_variables($type, t('created @entity', array('@entity' => drupal_strtolower($label)))),
  31. );
  32. $items[$type . '_update'] = $defaults + array(
  33. 'label' => t('After updating an existing @entity', array('@entity' => drupal_strtolower($label))),
  34. 'variables' => entity_rules_events_variables($type, t('updated @entity', array('@entity' => drupal_strtolower($label))), TRUE),
  35. );
  36. $items[$type . '_presave'] = $defaults + array(
  37. 'label' => t('Before saving a @entity', array('@entity' => drupal_strtolower($label))),
  38. 'variables' => entity_rules_events_variables($type, t('saved @entity', array('@entity' => drupal_strtolower($label))), TRUE),
  39. );
  40. $items[$type . '_delete'] = $defaults + array(
  41. 'label' => t('After deleting a @entity', array('@entity' => drupal_strtolower($label))),
  42. 'variables' => entity_rules_events_variables($type, t('deleted @entity', array('@entity' => drupal_strtolower($label)))),
  43. );
  44. if (count($info['view modes'])) {
  45. $items[$type . '_view'] = $defaults + array(
  46. 'label' => t('@entity is viewed', array('@entity' => $label)),
  47. 'variables' => entity_rules_events_variables($type, t('viewed @entity', array('@entity' => drupal_strtolower($label)))) + array(
  48. 'view_mode' => array(
  49. 'type' => 'text',
  50. 'label' => t('view mode'),
  51. 'options list' => 'rules_get_entity_view_modes',
  52. // Add the entity-type for the options list callback.
  53. 'options list entity type' => $type,
  54. ),
  55. ),
  56. );
  57. }
  58. // Specify that on presave the entity is saved anyway.
  59. $items[$type . '_presave']['variables'][$type]['skip save'] = TRUE;
  60. return $items;
  61. }
  62. }
  63. /**
  64. * Returns some parameter info suiting for the specified entity type.
  65. */
  66. function entity_rules_events_variables($type, $label, $update = FALSE) {
  67. $args = array(
  68. $type => array('type' => $type, 'label' => $label),
  69. );
  70. if ($update) {
  71. $args += array(
  72. $type . '_unchanged' => array(
  73. 'type' => $type,
  74. 'label' => t('unchanged entity'),
  75. 'handler' => 'rules_events_entity_unchanged',
  76. ),
  77. );
  78. }
  79. return $args;
  80. }
  81. /**
  82. * Implements hook_rules_event_info().
  83. */
  84. function entity_rules_event_info() {
  85. $items = array();
  86. foreach (entity_crud_get_info() as $type => $info) {
  87. // By default we enable the controller only for non-configuration.
  88. $configuration = !empty($info['configuration']) || !empty($info['exportable']);
  89. $info += array('rules controller class' => $configuration ? FALSE : 'EntityDefaultRulesController');
  90. if ($info['rules controller class']) {
  91. $controller = new $info['rules controller class']($type);
  92. $items += $controller->eventInfo();
  93. }
  94. }
  95. return $items;
  96. }
  97. /**
  98. * Rules integration access callback.
  99. */
  100. function entity_rules_integration_event_access($type, $event_name) {
  101. // Cut of _insert/_update/.. from the event name.
  102. $entity_type = substr($event_name, 0, strrpos($event_name, '_'));
  103. $result = entity_access('view', $entity_type);
  104. // If no access callback is given, just grant access for viewing.
  105. return isset($result) ? $result : TRUE;
  106. }