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