Projet

Général

Profil

Paste
Télécharger (4,15 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / entity / entity.rules.inc @ a45e4bc1

1
<?php
2

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

    
12
/**
13
 * Default controller for generating Rules integration.
14
 */
15
class EntityDefaultRulesController {
16

    
17
  protected $type, $info;
18

    
19
  public function __construct($type) {
20
    $this->type = $type;
21
    $this->info = entity_get_info($type);
22
  }
23

    
24
  public function eventInfo() {
25
    $info = $this->info;
26
    $type = $this->type;
27

    
28
    $label = $info['label'];
29
    $defaults = array(
30
      'module' => isset($info['module']) ? $info['module'] : 'entity',
31
      'group' => $label,
32
      'access callback' => 'entity_rules_integration_event_access',
33
    );
34

    
35
    $items[$type . '_insert'] = $defaults + array(
36
      'label' => t('After saving a new @entity', array('@entity' => drupal_strtolower($label))),
37
      'variables' => entity_rules_events_variables($type, t('created @entity', array('@entity' => drupal_strtolower($label)))),
38
    );
39
    $items[$type . '_update'] = $defaults + array(
40
      'label' => t('After updating an existing @entity', array('@entity' => drupal_strtolower($label))),
41
      'variables' => entity_rules_events_variables($type, t('updated @entity', array('@entity' => drupal_strtolower($label))), TRUE),
42
    );
43
    $items[$type . '_presave'] = $defaults + array(
44
      'label' => t('Before saving a @entity', array('@entity' => drupal_strtolower($label))),
45
      'variables' => entity_rules_events_variables($type, t('saved @entity', array('@entity' => drupal_strtolower($label))), TRUE),
46
    );
47
    $items[$type . '_delete'] = $defaults + array(
48
      'label' => t('After deleting a @entity', array('@entity' => drupal_strtolower($label))),
49
      'variables' => entity_rules_events_variables($type, t('deleted @entity', array('@entity' => drupal_strtolower($label)))),
50
    );
51
    if (count($info['view modes'])) {
52
      $items[$type . '_view'] = $defaults + array(
53
        'label' => t('@entity is viewed', array('@entity' => $label)),
54
        'variables' => entity_rules_events_variables($type, t('viewed @entity', array('@entity' => drupal_strtolower($label)))) + array(
55
          'view_mode' => array(
56
            'type' => 'text',
57
            'label' => t('view mode'),
58
            'options list' => 'rules_get_entity_view_modes',
59
             // Add the entity-type for the options list callback.
60
            'options list entity type' => $type,
61
          ),
62
        ),
63
      );
64
    }
65
    // Specify that on presave the entity is saved anyway.
66
    $items[$type . '_presave']['variables'][$type]['skip save'] = TRUE;
67
    return $items;
68
  }
69

    
70
}
71

    
72
/**
73
 * Returns some parameter info suiting for the specified entity type.
74
 */
75
function entity_rules_events_variables($type, $label, $update = FALSE) {
76
  $args = array(
77
    $type => array('type' => $type, 'label' => $label),
78
  );
79
  if ($update) {
80
    $args += array(
81
      $type . '_unchanged' => array(
82
        'type' => $type,
83
        'label' => t('unchanged entity'),
84
        'handler' => 'rules_events_entity_unchanged',
85
      ),
86
    );
87
  }
88
  return $args;
89
}
90

    
91
/**
92
 * Implements hook_rules_event_info().
93
 */
94
function entity_rules_event_info() {
95
  $items = array();
96
  foreach (entity_crud_get_info() as $type => $info) {
97
    // By default we enable the controller only for non-configuration.
98
    $configuration = !empty($info['configuration']) || !empty($info['exportable']);
99
    $info += array('rules controller class' => $configuration ? FALSE : 'EntityDefaultRulesController');
100
    if ($info['rules controller class']) {
101
      $controller = new $info['rules controller class']($type);
102
      $items += $controller->eventInfo();
103
    }
104
  }
105
  return $items;
106
}
107

    
108
/**
109
 * Rules integration access callback.
110
 */
111
function entity_rules_integration_event_access($type, $event_name) {
112
  // Cut of _insert/_update/.. from the event name.
113
  $entity_type = substr($event_name, 0, strrpos($event_name, '_'));
114

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