49ac468c9d33e889d38c2e7b1fcf41e2dbd4dcd1
[project/views.git] / modules / node / views_handler_field_node.inc
1 <?php
2 /**
3 * @file
4 * Contains the basic 'node' field handler.
5 */
6
7 /**
8 * Field handler to provide simple renderer that allows linking to a node.
9 * Definition terms:
10 * - link_to_node default: Should this field have the checkbox "link to node" enabled by default.
11 */
12 class views_handler_field_node extends views_handler_field {
13
14 function init(&$view, &$options) {
15 parent::init($view, $options);
16 // Don't add the additional fields to groupby
17 if (!empty($this->options['link_to_node']) && !$this->view->display_handler->use_group_by()) {
18 $this->additional_fields['nid'] = array('table' => 'node', 'field' => 'nid');
19 if (module_exists('translation')) {
20 $this->additional_fields['language'] = array('table' => 'node', 'field' => 'language');
21 }
22 }
23 }
24
25 function option_definition() {
26 $options = parent::option_definition();
27 $options['link_to_node'] = array('default' => isset($this->definition['link_to_node default']) ? $this->definition['link_to_node default'] : FALSE);
28 return $options;
29 }
30
31 /**
32 * Provide link to node option
33 */
34 function options_form(&$form, &$form_state) {
35 $form['link_to_node'] = array(
36 '#title' => t('Link this field to the original piece of content'),
37 '#description' => t("Enable to override this field's links."),
38 '#type' => 'checkbox',
39 '#default_value' => !empty($this->options['link_to_node']),
40 );
41
42 parent::options_form($form, $form_state);
43 }
44
45 /**
46 * Render whatever the data is as a link to the node.
47 *
48 * Data should be made XSS safe prior to calling this function.
49 */
50 function render_link($data, $values) {
51 if (!empty($this->options['link_to_node']) && !empty($this->additional_fields['nid'])) {
52 if ($data !== NULL && $data !== '') {
53 $this->options['alter']['make_link'] = TRUE;
54 $this->options['alter']['path'] = "node/" . $this->get_value($values, 'nid');
55 if (isset($this->aliases['language'])) {
56 $languages = language_list();
57 $language = $this->get_value($values, 'language');
58 if (isset($languages[$language])) {
59 $this->options['alter']['language'] = $languages[$language];
60 }
61 else {
62 unset($this->options['alter']['language']);
63 }
64 }
65 }
66 else {
67 $this->options['alter']['make_link'] = FALSE;
68 }
69 }
70 return $data;
71 }
72
73 function render($values) {
74 $value = $this->get_value($values);
75 return $this->render_link($this->sanitize_value($value), $values);
76 }
77 }