entity.rules.inc 3.03 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?php

/**
 * @file
 * Provides Rules integration for entities provided via the CRUD API.
 *
 * Rules automatically provides us with actions for CRUD and a suiting entity
 * data type. For events the controller automatically invokes Rules events once
 * Rules is active, so we just have to provide the appropriate info.
 */

/**
 * Default controller for generating Rules integration.
 */
class EntityDefaultRulesController {

  protected $type, $info;

  public function __construct($type) {
    $this->type = $type;
    $this->info = entity_get_info($type);
  }

  public function eventInfo() {
    $info = $this->info;
    $type = $this->type;

    $label = $info['label'];
    $defaults = array(
      'module' => isset($info['module']) ? $info['module'] : 'entity',
      'group' => $label,
32
      'access callback' => 'entity_rules_integration_event_access',
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    );

    $items[$type . '_insert'] = $defaults + array(
      'label' => t('After saving a new @entity', array('@entity' => drupal_strtolower($label))),
      'variables' => array(
        $type => array('type' => $type, 'label' => t('created @entity', array('@entity' => drupal_strtolower($label)))),
      ),
    );
    $items[$type . '_update'] = $defaults + array(
      'label' => t('After updating an existing @entity', array('@entity' => drupal_strtolower($label))),
      'variables' => array(
        $type => array('type' => $type, 'label' => t('updated @entity', array('@entity' => drupal_strtolower($label)))),
      ),
    );
    $items[$type . '_presave'] = $defaults + array(
      'label' => t('Before saving a @entity', array('@entity' => drupal_strtolower($label))),
      'variables' => array(
        $type => array('type' => $type, 'label' => t('saved @entity', array('@entity' => drupal_strtolower($label)))),
      ),
    );
    $items[$type . '_delete'] = $defaults + array(
      'label' => t('After deleting a @entity', array('@entity' => drupal_strtolower($label))),
      'variables' => array(
        $type => array('type' => $type, 'label' => t('deleted @entity', array('@entity' => drupal_strtolower($label)))),
      ),
    );
    // Specify that on presave the entity is saved anyway.
    $items[$type . '_presave']['variables'][$type]['skip save'] = TRUE;
    return $items;
  }

}

/**
 * Implements hook_rules_event_info().
 */
function entity_rules_event_info() {
  $items = array();
71 72
  foreach (entity_crud_get_info() as $type => $info) {
    $info += array('rules controller class' => 'EntityDefaultRulesController');
73 74 75 76
    if ($info['rules controller class']) {
      $controller = new $info['rules controller class']($type);
      $items += $controller->eventInfo();
    }
77 78 79 80 81 82 83
  }
  return $items;
}

/**
 * Rules integration access callback.
 */
84 85 86 87 88 89 90
function entity_rules_integration_event_access($type, $event_name) {
  // Cut of _insert/_update/.. from the event name.
  $entity_type = substr($event_name, 0, strrpos($event_name, '_'));

  $result = entity_access('view', $entity_type);
  // If no access callback is given, just grant access for viewing.
  return isset($result) ? $result : TRUE;
91
}