filefield_paths.tokens.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @file
  4. * Tokens for the File (Field) Paths module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. *
  9. * @return mixed
  10. */
  11. function filefield_paths_token_info() {
  12. $info['tokens']['file']['ffp-name-only'] = array(
  13. 'name' => t("File name"),
  14. 'description' => t("File name without extension."),
  15. );
  16. $info['tokens']['file']['ffp-name-only-original'] = array(
  17. 'name' => t("File name - original"),
  18. 'description' => t("File name without extension - original."),
  19. );
  20. $info['tokens']['file']['ffp-extension-original'] = array(
  21. 'name' => t("File extension - original"),
  22. 'description' => t("File extension - original."),
  23. );
  24. return $info;
  25. }
  26. /**
  27. * Implements hook_tokens().
  28. *
  29. * @param $type
  30. * @param $tokens
  31. * @param array $data
  32. *
  33. * @return array
  34. */
  35. function filefield_paths_tokens($type, $tokens, array $data = array()) {
  36. $url_options = array('absolute' => TRUE);
  37. if (isset($language)) {
  38. $url_options['language'] = $language;
  39. }
  40. $replacements = array();
  41. if ($type == 'file' && !empty($data['file'])) {
  42. $file = $data['file'];
  43. foreach ($tokens as $name => $original) {
  44. switch ($name) {
  45. case 'ffp-name-only':
  46. $basename = drupal_basename($file->filename);
  47. $extension = preg_match('/\.[^.]+$/', $basename, $matches) ? $matches[0] : NULL;
  48. $replacements[$original] = !is_null($extension) ? drupal_substr($basename, 0, drupal_strlen($basename) - drupal_strlen($extension)) : $basename;
  49. break;
  50. case 'ffp-name-only-original':
  51. $basename = drupal_basename($file->origname);
  52. $extension = preg_match('/\.[^.]+$/', $basename, $matches) ? $matches[0] : NULL;
  53. $replacements[$original] = !is_null($extension) ? drupal_substr($basename, 0, drupal_strlen($basename) - drupal_strlen($extension)) : $basename;
  54. break;
  55. case 'ffp-extension-original':
  56. $replacements[$original] = preg_match('/[^.]+$/', drupal_basename($file->origname), $matches) ? $matches[0] : NULL;
  57. break;
  58. }
  59. }
  60. }
  61. return $replacements;
  62. }