file_entity.tokens.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /**
  3. * @file
  4. * Token integration for the file_entity module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function file_entity_token_info() {
  10. // File type tokens.
  11. $info['types']['file-type'] = array(
  12. 'name' => t('File type'),
  13. 'description' => t('Tokens associated with file types.'),
  14. 'needs-data' => 'file_type',
  15. );
  16. $info['tokens']['file-type']['name'] = array(
  17. 'name' => t('Name'),
  18. 'description' => t('The name of the file type.'),
  19. );
  20. $info['tokens']['file-type']['machine-name'] = array(
  21. 'name' => t('Machine-readable name'),
  22. 'description' => t('The unique machine-readable name of the file type.'),
  23. );
  24. $info['tokens']['file-type']['count'] = array(
  25. 'name' => t('File count'),
  26. 'description' => t('The number of files belonging to the file type.'),
  27. );
  28. $info['tokens']['file-type']['edit-url'] = array(
  29. 'name' => t('Edit URL'),
  30. 'description' => t("The URL of the file type's edit page."),
  31. );
  32. // File tokens.
  33. $info['tokens']['file']['type'] = array(
  34. 'name' => t('File type'),
  35. 'description' => t('The file type of the file.'),
  36. 'type' => 'file-type',
  37. );
  38. return $info;
  39. }
  40. /**
  41. * Implements hook_tokens().
  42. */
  43. function file_entity_tokens($type, $tokens, array $data = array(), array $options = array()) {
  44. $replacements = array();
  45. $url_options = array('absolute' => TRUE);
  46. if (isset($options['language'])) {
  47. $url_options['language'] = $options['language'];
  48. $language_code = $options['language']->language;
  49. }
  50. else {
  51. $language_code = NULL;
  52. }
  53. $sanitize = !empty($options['sanitize']);
  54. // File tokens.
  55. if ($type == 'file' && !empty($data['file'])) {
  56. $file = $data['file'];
  57. foreach ($tokens as $name => $original) {
  58. switch ($name) {
  59. case 'type':
  60. if ($file_type = file_info_file_types($file->type)) {
  61. $replacements[$original] = $sanitize ? check_plain($file_type['label']) : $file_type['label'];
  62. }
  63. break;
  64. }
  65. }
  66. // Chained token relationships.
  67. if (($file_type_tokens = token_find_with_prefix($tokens, 'type')) && $file_type = file_info_file_types($file->type)) {
  68. $file_type['type'] = $file->type;
  69. $replacements += token_generate('file-type', $file_type_tokens, array('file_type' => $file_type), $options);
  70. }
  71. }
  72. // File type tokens.
  73. if ($type == 'file-type' && !empty($data['file_type'])) {
  74. $file_type = $data['file_type'];
  75. foreach ($tokens as $name => $original) {
  76. switch ($name) {
  77. case 'name':
  78. $replacements[$original] = $sanitize ? check_plain($file_type['label']) : $file_type['label'];
  79. break;
  80. case 'machine-name':
  81. // This is a machine name so does not ever need to be sanitized.
  82. $replacements[$original] = $file_type['type'];
  83. break;
  84. case 'count':
  85. $query = db_select('file_managed');
  86. $query->condition('type', $file_type['type']);
  87. $query->addTag('file_type_file_count');
  88. $count = $query->countQuery()->execute()->fetchField();
  89. $replacements[$original] = (int) $count;
  90. break;
  91. case 'edit-url':
  92. $replacements[$original] = url('admin/config/media/file-types/manage/' . $file_type['type'] . '/fields', $url_options);
  93. break;
  94. }
  95. }
  96. }
  97. return $replacements;
  98. }