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