1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Plugin to provide a node context. A node context is a node wrapped in a
|
6
|
* context object that can be utilized by anything that accepts contexts.
|
7
|
*/
|
8
|
|
9
|
/**
|
10
|
* Plugins are described by creating a $plugin array which will be used
|
11
|
* by the system that includes this file.
|
12
|
*/
|
13
|
$plugin = array(
|
14
|
'title' => t("Node"),
|
15
|
'description' => t('A node object.'),
|
16
|
'context' => 'ctools_context_create_node',
|
17
|
'edit form' => 'ctools_context_node_settings_form',
|
18
|
'defaults' => array('nid' => ''),
|
19
|
'keyword' => 'node',
|
20
|
'context name' => 'node',
|
21
|
'convert list' => 'ctools_context_node_convert_list',
|
22
|
'convert' => 'ctools_context_node_convert',
|
23
|
'placeholder form' => array(
|
24
|
'#type' => 'textfield',
|
25
|
'#description' => t('Enter the node ID of a node for this context.'),
|
26
|
),
|
27
|
// This context is deprecated and should not be usable in the UI.
|
28
|
'no ui' => TRUE,
|
29
|
'no required context ui' => TRUE,
|
30
|
'superceded by' => 'entity:node',
|
31
|
);
|
32
|
|
33
|
/**
|
34
|
* It's important to remember that $conf is optional here, because contexts
|
35
|
* are not always created from the UI.
|
36
|
*/
|
37
|
function ctools_context_create_node($empty, $data = NULL, $conf = FALSE) {
|
38
|
$context = new ctools_context('node');
|
39
|
$context->plugin = 'node';
|
40
|
|
41
|
if ($empty) {
|
42
|
return $context;
|
43
|
}
|
44
|
|
45
|
if ($conf) {
|
46
|
$nid = is_array($data) && isset($data['nid']) ? $data['nid'] : (is_object($data) ? $data->nid : 0);
|
47
|
|
48
|
if (module_exists('translation')) {
|
49
|
if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) {
|
50
|
$nid = $translation;
|
51
|
$reload = TRUE;
|
52
|
}
|
53
|
}
|
54
|
|
55
|
if (is_array($data) || !empty($reload)) {
|
56
|
$data = node_load($nid);
|
57
|
}
|
58
|
}
|
59
|
|
60
|
if (!empty($data)) {
|
61
|
$context->data = $data;
|
62
|
$context->title = $data->title;
|
63
|
$context->argument = $data->nid;
|
64
|
|
65
|
$context->restrictions['type'] = array($data->type);
|
66
|
return $context;
|
67
|
}
|
68
|
}
|
69
|
|
70
|
function ctools_context_node_settings_form($form, &$form_state) {
|
71
|
$conf = &$form_state['conf'];
|
72
|
|
73
|
$form['node'] = array(
|
74
|
'#title' => t('Enter the title or NID of a node'),
|
75
|
'#type' => 'textfield',
|
76
|
'#maxlength' => 512,
|
77
|
'#autocomplete_path' => 'ctools/autocomplete/node',
|
78
|
'#weight' => -10,
|
79
|
);
|
80
|
|
81
|
if (!empty($conf['nid'])) {
|
82
|
$info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject();
|
83
|
if ($info) {
|
84
|
$link = l(t("'%title' [node id %nid]", array('%title' => $info->title, '%nid' => $info->nid)), "node/$info->nid", array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
|
85
|
$form['node']['#description'] = t('Currently set to !link', array('!link' => $link));
|
86
|
}
|
87
|
}
|
88
|
|
89
|
$form['nid'] = array(
|
90
|
'#type' => 'value',
|
91
|
'#value' => $conf['nid'],
|
92
|
);
|
93
|
|
94
|
$form['set_identifier'] = array(
|
95
|
'#type' => 'checkbox',
|
96
|
'#default_value' => FALSE,
|
97
|
'#title' => t('Reset identifier to node title'),
|
98
|
'#description' => t('If checked, the identifier will be reset to the node title of the selected node.'),
|
99
|
);
|
100
|
|
101
|
return $form;
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Validate a node.
|
106
|
*/
|
107
|
function ctools_context_node_settings_form_validate($form, &$form_state) {
|
108
|
// Validate the autocomplete.
|
109
|
if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) {
|
110
|
form_error($form['node'], t('You must select a node.'));
|
111
|
return;
|
112
|
}
|
113
|
|
114
|
if (empty($form_state['values']['node'])) {
|
115
|
return;
|
116
|
}
|
117
|
|
118
|
$nid = $form_state['values']['node'];
|
119
|
$preg_matches = array();
|
120
|
$match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches);
|
121
|
if (!$match) {
|
122
|
$match = preg_match('/^id: (\d+)/', $nid, $preg_matches);
|
123
|
}
|
124
|
|
125
|
if ($match) {
|
126
|
$nid = $preg_matches[1];
|
127
|
}
|
128
|
if (is_numeric($nid)) {
|
129
|
$node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject();
|
130
|
}
|
131
|
else {
|
132
|
$node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject();
|
133
|
}
|
134
|
|
135
|
// Do not allow unpublished nodes to be selected by unprivileged users.
|
136
|
if (!$node || (empty($node->status) && !(user_access('administer nodes')))) {
|
137
|
form_error($form['node'], t('Invalid node selected.'));
|
138
|
}
|
139
|
else {
|
140
|
form_set_value($form['nid'], $node->nid, $form_state);
|
141
|
}
|
142
|
}
|
143
|
|
144
|
function ctools_context_node_settings_form_submit($form, &$form_state) {
|
145
|
if ($form_state['values']['set_identifier']) {
|
146
|
$node = node_load($form_state['values']['nid']);
|
147
|
$form_state['values']['identifier'] = $node->title;
|
148
|
}
|
149
|
|
150
|
// This will either be the value set previously or a value set by the
|
151
|
// validator.
|
152
|
$form_state['conf']['nid'] = $form_state['values']['nid'];
|
153
|
}
|
154
|
|
155
|
/**
|
156
|
* Provide a list of ways that this context can be converted to a string.
|
157
|
*/
|
158
|
function ctools_context_node_convert_list() {
|
159
|
$tokens = token_info();
|
160
|
foreach ($tokens['tokens']['node'] as $id => $info) {
|
161
|
if (!isset($list[$id])) {
|
162
|
$list[$id] = $info['name'];
|
163
|
}
|
164
|
}
|
165
|
|
166
|
return $list;
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Convert a context into a string.
|
171
|
*/
|
172
|
function ctools_context_node_convert($context, $type) {
|
173
|
$tokens = token_info();
|
174
|
if (isset($tokens['tokens']['node'][$type])) {
|
175
|
$values = token_generate('node', array($type => $type), array('node' => $context->data));
|
176
|
if (isset($values[$type])) {
|
177
|
return $values[$type];
|
178
|
}
|
179
|
}
|
180
|
}
|