filter_path_alias.module 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Implements hook_filter_info().
  4. */
  5. function filter_path_alias_filter_info() {
  6. $filters['path_alias'] = array(
  7. 'title' => t('Path Alias'),
  8. 'description' => t('replace internal machine links with alias'),
  9. 'process callback' => 'filter_path_alias_callback',
  10. );
  11. return $filters;
  12. }
  13. function filter_path_alias_callback($text, $filter, $format, $langcode, $cache, $cache_id){
  14. // dsm($text, 'text');
  15. // dsm($_SERVER, 'server');
  16. if (preg_match_all('/href="([^"]+)"/', $text, $matches_code)) {
  17. // dsm($matches_code, 'matches_code');
  18. foreach ($matches_code[1] as $i => $path) {
  19. $old_path = $path;
  20. $path = str_replace('http://', '', $path);
  21. $path = str_replace($_SERVER['SERVER_NAME'], '', $path);
  22. $path = preg_replace('/#.*$/', '', $path);
  23. $path = preg_replace('/\?.*$/', '', $path);
  24. $path = preg_replace('/^\//', '', $path);
  25. // dsm($path, 'path');
  26. if($path != ""){
  27. $alias = drupal_get_path_alias($path, $langcode);
  28. // dsm($alias, 'alias');
  29. if($alias != $path){
  30. $path_aliased = str_replace($old_path, $alias, $matches_code[0][$i]);
  31. $text = str_replace($matches_code[0][$i], $path_aliased, $text);
  32. }
  33. }
  34. }
  35. }
  36. return $text;
  37. }