views_handler_field_file.inc

  1. 3.x modules/system/views_handler_field_file.inc
  2. 2.x modules/system/views_handler_field_file.inc

File

modules/system/views_handler_field_file.inc
View source
  1. <?php
  2. /**
  3. * Field handler to provide simple renderer that allows linking to a file.
  4. */
  5. class views_handler_field_file extends views_handler_field {
  6. /**
  7. * Constructor to provide additional field to add.
  8. */
  9. function init(&$view, &$options) {
  10. parent::init($view, $options);
  11. if (!empty($options['link_to_file'])) {
  12. $this->additional_fields['filepath'] = 'filepath';
  13. }
  14. }
  15. function option_definition() {
  16. $options = parent::option_definition();
  17. $options['link_to_file'] = array('default' => FALSE);
  18. return $options;
  19. }
  20. /**
  21. * Provide link to file option
  22. */
  23. function options_form(&$form, &$form_state) {
  24. parent::options_form($form, $form_state);
  25. $form['link_to_file'] = array(
  26. '#title' => t('Link this field to download the file'),
  27. '#description' => t('This will override any other link you have set.'),
  28. '#type' => 'checkbox',
  29. '#default_value' => !empty($this->options['link_to_file']),
  30. );
  31. }
  32. /**
  33. * Render whatever the data is as a link to the file.
  34. *
  35. * Data should be made XSS safe prior to calling this function.
  36. */
  37. function render_link($data, $values) {
  38. if (!empty($this->options['link_to_file']) && $data !== NULL && $data !== '') {
  39. $this->options['alter']['make_link'] = TRUE;
  40. $this->options['alter']['path'] = file_create_url($values->{$this->aliases['filepath']});
  41. }
  42. return $data;
  43. }
  44. function render($values) {
  45. return $this->render_link(check_plain($values->{$this->field_alias}), $values);
  46. }
  47. }