Projet

Général

Profil

Paste
Télécharger (6,37 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid.module @ a2bb1a14

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Main module functions for the uuid module.
6
 */
7
8
/**
9
 * Include main API functions.
10
 */
11
module_load_include('inc', 'uuid', 'uuid');
12
13
/**
14
 * Include the Entity API implementation of the UUID API.
15
 */
16
module_load_include('inc', 'uuid', 'uuid.entity');
17
18
/**
19
 * Include implementations for all core modules.
20
 *
21
 * Instead of having separate modules for all core implementations this file
22
 * implements all of them in their own name space. In some cases this adds some
23
 * unecessary code weight, but instead it saves us from module mania.
24
 */
25
module_load_include('inc', 'uuid', 'uuid.core');
26
27
/**
28 e7101f36 Julien Enselme
 * Implements hook_menu().
29 85ad3d82 Assos Assos
 */
30
function uuid_menu() {
31
  $items = array();
32
33
  $items['uuid'] = array(
34
    'title' => 'UUID redirector',
35
    'description' => 'Redirects requests for UUID URIs to the referenced entity.',
36
    'page callback' => 'uuid_redirector',
37 4eeb3b46 Assos Assos
    // The access check is handled in the page callback.
38 85ad3d82 Assos Assos
    'access callback' => TRUE,
39
    'type' => MENU_CALLBACK,
40
  );
41
42
  $items['admin/config/system/uuid'] = array(
43
    'title' => 'Universally unique identifiers',
44
    'description' => 'Configure universally unique identifiers.',
45
    'page callback' => 'drupal_get_form',
46
    'page arguments' => array('uuid_admin_form'),
47
    'access arguments' => array('administer uuid'),
48
    'file' => 'uuid.admin.inc',
49
  );
50
51
  // Conditional support for Devel. A good thing for developers.
52
  if (module_exists('devel')) {
53
    $entity_types = array(
54
      'user' => array('path' => 'user/%user/devel/load-by-uuid', 'arg' => 1),
55
      'node' => array('path' => 'node/%node/devel/load-by-uuid', 'arg' => 1),
56
      'comment' => array('path' => 'comment/%comment/devel/load-by-uuid', 'arg' => 1),
57
      'taxonomy_term' => array('path' => 'taxonomy/term/%taxonomy_term/devel/load-by-uuid', 'arg' => 2),
58
    );
59
    foreach ($entity_types as $entity_type => $info) {
60
      $items[$info['path']] = array(
61
        'title' => 'Load by UUID',
62
        'page callback' => 'uuid_devel_load_by_uuid',
63
        'page arguments' => array($entity_type, $info['arg']),
64
        'access arguments' => array('access devel information'),
65
        'type' => MENU_LOCAL_TASK,
66
        'file' => 'uuid.admin.inc',
67
        'weight' => 100,
68
      );
69
    }
70
  }
71
  return $items;
72
}
73
74
/**
75 e7101f36 Julien Enselme
 * Implements hook_ctools_plugin_directory().
76 85ad3d82 Assos Assos
 */
77
function uuid_ctools_plugin_directory($module, $plugin) {
78
  if ($module == 'ctools') {
79
    return 'plugins/' . $plugin;
80
  }
81
}
82
83
/**
84 e7101f36 Julien Enselme
 * Implements hook_permission().
85 85ad3d82 Assos Assos
 */
86
function uuid_permission() {
87
  return array(
88
    'administer uuid' => array(
89
      'title' => t('Administer UUID'),
90
      'description' => t('Allows configuration of the UUID module and APIs.'),
91
    ),
92
  );
93
}
94
95
/**
96
 * Implements hook_hook_info().
97
 */
98
function uuid_hook_info() {
99
  $hook_names = array(
100
    'uuid_info',
101
    'uuid_sync',
102
    'entity_uuid_load',
103
    'field_uuid_load',
104
    'entity_uuid_presave',
105
    'entity_uuid_save',
106
    'entity_uuid_delete',
107
    'field_uuid_presave',
108
    'uuid_menu_path_to_uri_alter',
109
    'uuid_menu_uri_to_path_alter',
110
    'uuid_default_entities',
111
    'uuid_entities_pre_rebuild',
112
    'uuid_entities_pre_revert',
113
    'uuid_entities_post_rebuild',
114
    'uuid_entities_post_revert',
115
    'uuid_entities_features_export_entity_alter',
116
    'uuid_entities_features_export_field_alter',
117
    'uuid_uri_data',
118
    'uuid_id_uri_data',
119
    'uuid_uri_data',
120
    'uuid_id_uri_data',
121
  );
122
123
  return array_fill_keys($hook_names, array('group' => 'uuid'));
124
}
125
126
/**
127 e7101f36 Julien Enselme
 * Implements hook_views_api().
128 85ad3d82 Assos Assos
 */
129
function uuid_views_api() {
130
  return array(
131
    'api' => 2,
132
    'path' => drupal_get_path('module', 'uuid'),
133
  );
134
}
135
136
/**
137 e7101f36 Julien Enselme
 * Implements hook_module_implements_alter().
138 85ad3d82 Assos Assos
 *
139 e7101f36 Julien Enselme
 * Moves hook_entity_info_alter() implementation to the bottom so it is
140 85ad3d82 Assos Assos
 * invoked after all modules relying on the entity API.
141
 *
142
 * @see uuid_entity_info_alter()
143
 */
144 e7101f36 Julien Enselme
function uuid_module_implements_alter(&$implementss, $hook) {
145 85ad3d82 Assos Assos
  if ($hook == 'entity_info_alter') {
146
    // Move our hook Implements to the bottom.
147 e7101f36 Julien Enselme
    $group = $implementss['uuid'];
148
    unset($implementss['uuid']);
149
    $implementss['uuid'] = $group;
150 85ad3d82 Assos Assos
  }
151
}
152
153
/**
154 e7101f36 Julien Enselme
 * Implements hook_uuid_sync().
155 85ad3d82 Assos Assos
 */
156
function uuid_uuid_sync() {
157 4eeb3b46 Assos Assos
  foreach (entity_get_info() as $info) {
158 85ad3d82 Assos Assos
    if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
159
      _uuid_sync_table($info['base table'], $info['entity keys']['id'], $info['entity keys']['uuid']);
160
      if (!empty($info['entity keys']['revision uuid'])) {
161
        _uuid_sync_table($info['revision table'], $info['entity keys']['revision'], $info['entity keys']['revision uuid']);
162
      }
163
    }
164
  }
165
}
166
167
/**
168
 * Helper function that executes the update on the actual table.
169
 */
170
function _uuid_sync_table($table, $id_field, $uuid_field) {
171
  // Fetch empty records.
172
  $result = db_select($table, 't')
173
    ->fields('t', array($id_field))
174
    ->condition(db_or()->condition($uuid_field, '')->isNull($uuid_field))
175
    ->execute();
176
177
  // Update empty records.
178
  foreach ($result as $record) {
179
    db_update($table)
180
      ->fields(array($uuid_field => uuid_generate()))
181
      ->condition($id_field, $record->{$id_field})
182
      ->execute();
183
  }
184
}
185
186
/**
187 e7101f36 Julien Enselme
 * Implements hook_features_api().
188 85ad3d82 Assos Assos
 *
189
 * The Features support consists of exporting entities from a Deploy
190
 * <em>fetch-only</em> plan. Deploy is only required to generate the feature
191
 * it self.
192
 *
193
 * The reason why we depend on Deploy for the generation of the content is
194
 * because Deploy has the kind of dependency detection framework we need, to
195
 * identify all dependencies for all entities.
196
 */
197
function uuid_features_api() {
198
  return array(
199
    'uuid_entities' => array(
200
      'name' => t('UUID entities'),
201
      'default_hook' => 'uuid_default_entities',
202
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
203
      'feature_source' => TRUE,
204 e7101f36 Julien Enselme
      'file' => drupal_get_path('module', 'uuid') . '/uuid.features.inc',
205 85ad3d82 Assos Assos
    ),
206
  );
207
}
208
209
/**
210
 * Redirects all UUID URI requests to the appropriate entity page.
211
 */
212
function uuid_redirector() {
213
  $entity_data = uuid_uri_array_to_data(arg());
214 e7101f36 Julien Enselme
215 85ad3d82 Assos Assos
  $entity_info = entity_get_info($entity_data['entity_type']);
216
  if (empty($entity_info['uuid'])) {
217 73a44071 Assos Assos
    return MENU_NOT_FOUND;
218 85ad3d82 Assos Assos
  }
219
220
  $entities = entity_uuid_load($entity_data['entity_type'], array($entity_data['uuid']));
221
  if (!count($entities)) {
222 73a44071 Assos Assos
    return MENU_NOT_FOUND;
223 85ad3d82 Assos Assos
  }
224
225
  $uri = entity_uri($entity_data['entity_type'], current($entities));
226 73a44071 Assos Assos
227
  if (!drupal_valid_path($uri['path'])) {
228
    return MENU_ACCESS_DENIED;
229
  }
230
231
  drupal_goto($uri['path'], $uri['options'], 301);
232 85ad3d82 Assos Assos
}