Projet

Général

Profil

Paste
Télécharger (1,96 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / uuid / uuid_path / uuid_path.module @ bf6fb0ee

1
<?php
2

    
3
/**
4
 * @file
5
 * UUID path module functions.
6
 */
7

    
8
/**
9
 * Implements hook_entity_uuid_load().
10
 */
11
function uuid_path_entity_uuid_load(&$entities, $entity_type) {
12
  _uuid_path_load_url_aliases($entities, $entity_type);
13
}
14

    
15
/**
16
 * Implements hook_entity_uuid_save().
17
 */
18
function uuid_path_entity_uuid_save(&$entity, $entity_type) {
19
  _uuid_path_save_url_aliases($entity, $entity_type);
20
}
21

    
22
/**
23
 * Loads url aliases in the corresponding entity.
24
 */
25
function _uuid_path_load_url_aliases(&$entities, $entity_type) {
26
  $info = entity_get_info($entity_type);
27
  // We only care about entities with URLs.
28
  if (!isset($info['uri callback'])) {
29
    return;
30
  }
31

    
32
  $callback = $info['uri callback'];
33
  foreach ($entities as $id => $entity) {
34
    $path = $callback($entity);
35
    $aliases = _uuid_path_url_alias_load($path['path']);
36

    
37
    // Ignore local IDs.
38
    foreach ($aliases as &$alias) {
39
      unset($alias->pid);
40
      unset($alias->source);
41
    }
42

    
43
    $entities[$id]->url_alias = $aliases;
44
  }
45
}
46

    
47
/**
48
 * Saves the received url aliases.
49
 */
50
function _uuid_path_save_url_aliases(&$entity, $entity_type) {
51
  $info = entity_get_info($entity_type);
52

    
53
  // We only care when there is a url callback.
54
  if (!isset($info['uri callback'])) {
55
    return FALSE;
56
  }
57

    
58
  $callback = $info['uri callback'];
59
  $uri = $callback($entity);
60
  $path = $uri['path'];
61

    
62
  // Delete existing aliases.
63
  path_delete(array('source' => $path));
64

    
65
  // Continue if aliases are present.
66
  if (empty($entity->url_alias)) {
67
    return FALSE;
68
  }
69

    
70
  foreach ($entity->url_alias as $alias) {
71
    $entry = (array) $alias;
72
    $entry['source'] = $path;
73
    path_save($entry);
74
  }
75
}
76

    
77
/**
78
 * Loads all aliases associated with a path.
79
 *
80
 * @param string $path
81
 *   The source path to look up.
82
 *
83
 * @return array
84
 *   Array of paths or NULL if none found.
85
 */
86
function _uuid_path_url_alias_load($path) {
87
  return db_select('url_alias')
88
    ->condition('source', $path)
89
    ->fields('url_alias')
90
    ->execute()
91
    ->fetchAll(PDO::FETCH_OBJ);
92
}