Projet

Général

Profil

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

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

1
<?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
 * Implements hook_menu().
29
 */
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
    'access callback' => TRUE,
38
    'type' => MENU_CALLBACK,
39
  );
40

    
41
  $items['admin/config/system/uuid'] = array(
42
    'title' => 'Universally unique identifiers',
43
    'description' => 'Configure universally unique identifiers.',
44
    'page callback' => 'drupal_get_form',
45
    'page arguments' => array('uuid_admin_form'),
46
    'access arguments' => array('administer uuid'),
47
    'type' => MENU_NORMAL_ITEM,
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
 * Implements hook_ctools_plugin_directory().
76
 */
77
function uuid_ctools_plugin_directory($module, $plugin) {
78
  if ($module == 'ctools') {
79
    return 'plugins/' . $plugin;
80
  }
81
}
82

    
83
/**
84
 * Implements hook_permission().
85
 */
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
/**
128
 * Implements hook_views_api().
129
 */
130
function uuid_views_api() {
131
  return array(
132
    'api' => 2,
133
    'path' => drupal_get_path('module', 'uuid'),
134
  );
135
}
136

    
137
/**
138
 * Implements hook_module_implements_alter().
139
 *
140
 * Moves hook_entity_info_alter() implementation to the bottom so it is
141
 * invoked after all modules relying on the entity API.
142
 *
143
 * @see uuid_entity_info_alter()
144
 */
145
function uuid_module_implements_alter(&$implementss, $hook) {
146
  if ($hook == 'entity_info_alter') {
147
    // Move our hook Implements to the bottom.
148
    $group = $implementss['uuid'];
149
    unset($implementss['uuid']);
150
    $implementss['uuid'] = $group;
151
  }
152
}
153

    
154
/**
155
 * Implements hook_uuid_sync().
156
 */
157
function uuid_uuid_sync() {
158
  foreach (entity_get_info() as $entity_type => $info) {
159
    if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
160
      _uuid_sync_table($info['base table'], $info['entity keys']['id'], $info['entity keys']['uuid']);
161
      if (!empty($info['entity keys']['revision uuid'])) {
162
        _uuid_sync_table($info['revision table'], $info['entity keys']['revision'], $info['entity keys']['revision uuid']);
163
      }
164
    }
165
  }
166
}
167

    
168
/**
169
 * Helper function that executes the update on the actual table.
170
 */
171
function _uuid_sync_table($table, $id_field, $uuid_field) {
172
  // Fetch empty records.
173
  $result = db_select($table, 't')
174
    ->fields('t', array($id_field))
175
    ->condition(db_or()->condition($uuid_field, '')->isNull($uuid_field))
176
    ->execute();
177

    
178
  // Update empty records.
179
  foreach ($result as $record) {
180
    db_update($table)
181
      ->fields(array($uuid_field => uuid_generate()))
182
      ->condition($id_field, $record->{$id_field})
183
      ->execute();
184
  }
185
}
186

    
187
/**
188
 * Implements hook_features_api().
189
 *
190
 * The Features support consists of exporting entities from a Deploy
191
 * <em>fetch-only</em> plan. Deploy is only required to generate the feature
192
 * it self.
193
 *
194
 * The reason why we depend on Deploy for the generation of the content is
195
 * because Deploy has the kind of dependency detection framework we need, to
196
 * identify all dependencies for all entities.
197
 */
198
function uuid_features_api() {
199
  return array(
200
    'uuid_entities' => array(
201
      'name' => t('UUID entities'),
202
      'default_hook' => 'uuid_default_entities',
203
      'default_file' => FEATURES_DEFAULTS_INCLUDED,
204
      'feature_source' => TRUE,
205
      'file' => drupal_get_path('module', 'uuid') . '/uuid.features.inc',
206
    ),
207
  );
208
}
209

    
210
/**
211
 * Redirects all UUID URI requests to the appropriate entity page.
212
 */
213
function uuid_redirector() {
214
  $entity_data = uuid_uri_array_to_data(arg());
215

    
216
  $entity_info = entity_get_info($entity_data['entity_type']);
217
  if (empty($entity_info['uuid'])) {
218
    return drupal_not_found();
219
  }
220

    
221
  $entities = entity_uuid_load($entity_data['entity_type'], array($entity_data['uuid']));
222
  if (!count($entities)) {
223
    return drupal_not_found();
224
  }
225

    
226
  $uri = entity_uri($entity_data['entity_type'], current($entities));
227
  drupal_goto($uri['path'], array(), 301);
228
}
229