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

Definition of views_handler_field_file.

File

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