uuid_path.module 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @file
  4. * UUID path module functions.
  5. */
  6. /**
  7. * Implements hook_entity_uuid_load().
  8. */
  9. function uuid_path_entity_uuid_load(&$entities, $entity_type) {
  10. _uuid_path_load_url_aliases($entities, $entity_type);
  11. }
  12. /**
  13. * Implements hook_entity_uuid_save().
  14. */
  15. function uuid_path_entity_uuid_save(&$entity, $entity_type) {
  16. _uuid_path_save_url_aliases($entity, $entity_type);
  17. }
  18. /**
  19. * Loads url aliases in the corresponding entity.
  20. */
  21. function _uuid_path_load_url_aliases(&$entities, $entity_type) {
  22. $info = entity_get_info($entity_type);
  23. // We only care about entities with URLs.
  24. if (!isset($info['uri callback'])) {
  25. return;
  26. }
  27. $callback = $info['uri callback'];
  28. foreach ($entities as $id => $entity) {
  29. $path = $callback($entity);
  30. $aliases = _uuid_path_url_alias_load($path['path']);
  31. // Ignore local IDs.
  32. foreach ($aliases as &$alias) {
  33. unset($alias->pid);
  34. unset($alias->source);
  35. }
  36. $entities[$id]->url_alias = $aliases;
  37. }
  38. }
  39. /**
  40. * Saves the received url aliases.
  41. */
  42. function _uuid_path_save_url_aliases(&$entity, $entity_type) {
  43. $info = entity_get_info($entity_type);
  44. // We only care when there is a url callback.
  45. if (!isset($info['uri callback'])) {
  46. return FALSE;
  47. }
  48. $callback = $info['uri callback'];
  49. $uri = $callback($entity);
  50. $path = $uri['path'];
  51. // Delete existing aliases.
  52. path_delete(array('source' => $path));
  53. // Continue if aliases are present.
  54. if (empty($entity->url_alias)) {
  55. return FALSE;
  56. }
  57. foreach ($entity->url_alias as $alias) {
  58. $entry = (array) $alias;
  59. $entry['source'] = $path;
  60. path_save($entry);
  61. }
  62. }
  63. /**
  64. * Loads all aliases associated with a path.
  65. *
  66. * @param string $path
  67. * The source path to look up.
  68. *
  69. * @return array
  70. * Array of paths or NULL if none found.
  71. */
  72. function _uuid_path_url_alias_load($path) {
  73. return db_select('url_alias')
  74. ->condition('source', $path)
  75. ->fields('url_alias')
  76. ->execute()
  77. ->fetchAll(PDO::FETCH_OBJ);
  78. }