views_handler_field_file_extension.inc

Definition of views_handler_field_file_extension.

File

modules/system/views_handler_field_file_extension.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_file_extension.
  5. */
  6. /**
  7. * Returns a pure file extension of the file, for example 'module'.
  8. * @ingroup views_field_handlers
  9. */
  10. class views_handler_field_file_extension extends views_handler_field {
  11. public function option_definition() {
  12. $options = parent::option_definition();
  13. $options['extension_detect_tar'] = array('default' => FALSE, 'bool' => TRUE);
  14. return $options;
  15. }
  16. public function options_form(&$form, &$form_state) {
  17. parent::options_form($form, $form_state);
  18. $form['extension_detect_tar'] = array(
  19. '#type' => 'checkbox',
  20. '#title' => t('Detect if tar is part of the extension'),
  21. '#description' => t("See if the previous extension is '.tar' and if so, add that, so we see 'tar.gz' or 'tar.bz2' instead of just 'gz'."),
  22. '#default_value' => $this->options['extension_detect_tar'],
  23. );
  24. }
  25. function render($values) {
  26. $value = $this->get_value($values);
  27. if (!$this->options['extension_detect_tar']) {
  28. if (preg_match('/\.([^\.]+)$/', $value, $match)) {
  29. return $this->sanitize_value($match[1]);
  30. }
  31. }
  32. else {
  33. $file_parts = explode('.', basename($value));
  34. // If there is an extension.
  35. if (count($file_parts) > 1) {
  36. $extension = array_pop($file_parts);
  37. $last_part_in_name = array_pop($file_parts);
  38. if ($last_part_in_name === 'tar') {
  39. $extension = 'tar.' . $extension;
  40. }
  41. return $this->sanitize_value($extension);
  42. }
  43. }
  44. }
  45. }