url_alter_test.module 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @file
  4. * Module to help test hook_url_inbound_alter() and hook_url_outbound_alter().
  5. */
  6. /**
  7. * Implements hook_menu().
  8. */
  9. function url_alter_test_menu() {
  10. $items['url-alter-test/foo'] = array(
  11. 'title' => 'Foo',
  12. 'page callback' => 'url_alter_test_foo',
  13. 'access arguments' => array('access content'),
  14. 'type' => MENU_CALLBACK,
  15. );
  16. return $items;
  17. }
  18. /**
  19. * Menu callback.
  20. */
  21. function url_alter_test_foo() {
  22. print 'current_path=' . current_path() . ' request_path=' . request_path();
  23. exit;
  24. }
  25. /**
  26. * Implements hook_url_inbound_alter().
  27. */
  28. function url_alter_test_url_inbound_alter(&$path, $original_path, $path_language) {
  29. if (!request_path() && !empty($_GET['q'])) {
  30. drupal_set_message("\$_GET['q'] is non-empty with an empty request path.");
  31. }
  32. // Rewrite user/username to user/uid.
  33. if (preg_match('!^user/([^/]+)(/.*)?!', $path, $matches)) {
  34. if ($account = user_load_by_name($matches[1])) {
  35. $matches += array(2 => '');
  36. $path = 'user/' . $account->uid . $matches[2];
  37. }
  38. }
  39. // Rewrite community/ to forum/.
  40. if ($path == 'community' || strpos($path, 'community/') === 0) {
  41. $path = 'forum' . substr($path, 9);
  42. }
  43. if ($path == 'url-alter-test/bar') {
  44. $path = 'url-alter-test/foo';
  45. }
  46. }
  47. /**
  48. * Implements hook_url_outbound_alter().
  49. */
  50. function url_alter_test_url_outbound_alter(&$path, &$options, $original_path) {
  51. // Rewrite user/uid to user/username.
  52. if (preg_match('!^user/([0-9]+)(/.*)?!', $path, $matches)) {
  53. if ($account = user_load($matches[1])) {
  54. $matches += array(2 => '');
  55. $path = 'user/' . $account->name . $matches[2];
  56. }
  57. }
  58. // Rewrite forum/ to community/.
  59. if ($path == 'forum' || strpos($path, 'forum/') === 0) {
  60. $path = 'community' . substr($path, 5);
  61. }
  62. }