pathologic.api.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by Pathologic.
  5. *
  6. * @ingroup pathologic
  7. */
  8. /**
  9. * @addtogroup hooks
  10. * @{
  11. */
  12. /**
  13. * Allow modules to alter a URL Pathologic is about to create.
  14. *
  15. * This hook is invoked after Pathologic has torn apart a URL it thinks it can
  16. * alter properly and is just about to call the url() function to construct the
  17. * new URL. Modules can alter the values that Pathologic is about to send to
  18. * url(), or even stop Pathologic from altering a URL entirely.
  19. *
  20. * @param $url_params
  21. * An array with 'path' and 'options' values, which correspond to the $path
  22. * and $options parameters of the url() function. The 'options' array has an
  23. * extra parameter labeled 'use_original' which is set to FALSE by default.
  24. * This parameter is ignored by url(), but if its value is set to TRUE after
  25. * all alter hook invocations, Pathologic will return the original, unaltered
  26. * path it found in the content instead of calling url() and generating a new
  27. * one. Thus, it provides a way for modules to halt the alteration of paths
  28. * which Pathologic has incorrectly decided should be altered.
  29. * @param $parts
  30. * This array contains the result of running parse_url() on the path that
  31. * Pathologic found in content, though Pathologic likely altered some of the
  32. * values in this array since. It contains another parameter, 'original',
  33. * which contains the original URL Pathologic found in the content, unaltered.
  34. * You should not alter this value in any way; to alter how Pathologic
  35. * constructs the new URL, alter $url_params instead.
  36. * @param $settings
  37. * This contains the settings Pathologic is using to decide how to alter the
  38. * URL; some settings are from the graphical filter form and alterable by the
  39. * user, while others are determined programmatically. If you're looking for
  40. * the filter settings which Pathologic is currently using (if you've altered
  41. * your own field onto the filter settings form, for example), try looking in
  42. * $settings['current_settings'].
  43. *
  44. * @see url()
  45. * @see parse_url()
  46. * @see pathologic_replace()
  47. * @see http://drupal.org/node/1762022
  48. */
  49. function hook_pathologic_alter(&$url_params, $parts, $settings) {
  50. // If we're linking to the "bananas" subdirectory or something under it, then
  51. // have Pathologic pass through the original URL, without altering it.
  52. if (preg_match('~^bananas(/.*)?$~', $url_params['path'])) {
  53. $url_params['options']['use_original'] = TRUE;
  54. }
  55. // If we're linking to a path like "article/something.html", then prepend
  56. // "magazine" to the path, but remove the ".html". The end result will look
  57. // like "magazine/article/something".
  58. if (preg_match('~^article/(.+)\.html$~', $url_params['path'], $matches)) {
  59. $url_params['path'] = 'magazine/article/' . $matches[1];
  60. }
  61. // If the URL doesn't have a "foo" query parameter, then add one.
  62. if (!is_array($url_params['options']['query'])) {
  63. $url_params['options']['query'] = array();
  64. }
  65. if (empty($url_params['options']['query']['foo'])) {
  66. $url_params['options']['query']['foo'] = 'bar';
  67. }
  68. // If it's a path to a local image, make sure it's using our CDN server.
  69. if (preg_match('~\.(png|gif|jpe?g)$', $url_params['path'])) {
  70. $url_params['path'] = 'http://cdn.example.com/' . $url_params['path'];
  71. $url_params['options']['external'] = TRUE;
  72. }
  73. }
  74. /**
  75. * @} End of "addtogroup hooks".
  76. */