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 | |
---|
16 | function option_definition() { |
---|
17 | $options = parent::option_definition(); |
---|
18 | $options['link_to_file'] = array('default' => FALSE); |
---|
19 | return $options; |
---|
20 | } |
---|
21 | |
---|
22 | /** |
---|
23 | * Provide link to file option |
---|
24 | */ |
---|
25 | function options_form(&$form, &$form_state) { |
---|
26 | parent::options_form($form, $form_state); |
---|
27 | $form['link_to_file'] = array( |
---|
28 | '#title' => t('Link this field to download the file'), |
---|
29 | '#description' => t('This will override any other link you have set.'), |
---|
30 | '#type' => 'checkbox', |
---|
31 | '#default_value' => !empty($this->options['link_to_file']), |
---|
32 | ); |
---|
33 | } |
---|
34 | |
---|
35 | /** |
---|
36 | * Render whatever the data is as a link to the file. |
---|
37 | * |
---|
38 | * Data should be made XSS safe prior to calling this function. |
---|
39 | */ |
---|
40 | function render_link($data, $values) { |
---|
41 | if (!empty($this->options['link_to_file']) && $data !== NULL && $data !== '') { |
---|
42 | $this->options['alter']['make_link'] = TRUE; |
---|
43 | $this->options['alter']['path'] = file_create_url($values->{$this->aliases['filepath']}); |
---|
44 | } |
---|
45 | |
---|
46 | return $data; |
---|
47 | } |
---|
48 | |
---|
49 | function render($values) { |
---|
50 | return $this->render_link(check_plain($values->{$this->field_alias}), $values); |
---|
51 | } |
---|
52 | } |
---|