Mercurial > hg > rr-repo
comparison sites/all/modules/ctools/plugins/contexts/node_edit_form.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_edit_form context | |
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 edit form"), | |
15 'description' => t('A node edit form.'), | |
16 'context' => 'ctools_context_create_node_edit_form', | |
17 'edit form' => 'ctools_context_node_edit_form_settings_form', | |
18 'defaults' => array('nid' => ''), | |
19 'keyword' => 'node_edit', | |
20 'context name' => 'node_edit_form', | |
21 'convert list' => 'ctools_context_node_edit_convert_list', | |
22 'convert' => 'ctools_context_node_edit_convert', | |
23 'placeholder form' => array( | |
24 '#type' => 'textfield', | |
25 '#description' => t('Enter the node ID of a node for this argument:'), | |
26 ), | |
27 ); | |
28 | |
29 /** | |
30 * It's important to remember that $conf is optional here, because contexts | |
31 * are not always created from the UI. | |
32 */ | |
33 function ctools_context_create_node_edit_form($empty, $node = NULL, $conf = FALSE) { | |
34 static $creating = FALSE; | |
35 $context = new ctools_context(array('form', 'node_edit', 'node_form', 'node_edit_form', 'node', 'entity:node')); | |
36 $context->plugin = 'node_edit_form'; | |
37 | |
38 if ($empty || ($creating)) { | |
39 return $context; | |
40 } | |
41 $creating = TRUE; | |
42 | |
43 if ($conf) { | |
44 // In this case, $node is actually our $conf array. | |
45 $nid = is_array($node) && isset($node['nid']) ? $node['nid'] : (is_object($node) ? $node->nid : 0); | |
46 | |
47 if (module_exists('translation')) { | |
48 if ($translation = module_invoke('translation', 'node_nid', $nid, $GLOBALS['language']->language)) { | |
49 $nid = $translation; | |
50 $reload = TRUE; | |
51 } | |
52 } | |
53 | |
54 if (is_array($node) || !empty($reload)) { | |
55 $node = node_load($nid); | |
56 } | |
57 } | |
58 | |
59 if (!empty($node)) { | |
60 $form_id = $node->type . '_node_form'; | |
61 | |
62 $form_state = array('want form' => TRUE, 'build_info' => array('args' => array($node))); | |
63 | |
64 $file = drupal_get_path('module', 'node') . '/node.pages.inc'; | |
65 require_once DRUPAL_ROOT . '/' . $file; | |
66 // This piece of information can let other modules know that more files | |
67 // need to be included if this form is loaded from cache: | |
68 $form_state['build_info']['files'] = array($file); | |
69 | |
70 $form = drupal_build_form($form_id, $form_state); | |
71 | |
72 // Fill in the 'node' portion of the context | |
73 $context->data = $node; | |
74 $context->title = isset($node->title) ? $node->title : ''; | |
75 $context->argument = isset($node->nid) ? $node->nid : $node->type; | |
76 | |
77 $context->form = $form; | |
78 $context->form_state = &$form_state; | |
79 $context->form_id = $form_id; | |
80 $context->form_title = isset($node->title) ? $node->title : ''; | |
81 $context->node_type = $node->type; | |
82 $context->restrictions['type'] = array($node->type); | |
83 $context->restrictions['form'] = array('form'); | |
84 | |
85 $creating = FALSE; | |
86 return $context; | |
87 } | |
88 $creating = FALSE; | |
89 } | |
90 | |
91 function ctools_context_node_edit_form_settings_form($form, &$form_state) { | |
92 $conf = &$form_state['conf']; | |
93 | |
94 $form['node'] = array( | |
95 '#title' => t('Enter the title or NID of a node'), | |
96 '#type' => 'textfield', | |
97 '#maxlength' => 512, | |
98 '#autocomplete_path' => 'ctools/autocomplete/node', | |
99 '#weight' => -10, | |
100 ); | |
101 | |
102 if (!empty($conf['nid'])) { | |
103 $info = db_query('SELECT * FROM {node} WHERE nid = :nid', array(':nid' => $conf['nid']))->fetchObject(); | |
104 if ($info) { | |
105 $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)); | |
106 $form['node']['#description'] = t('Currently set to !link', array('!link' => $link)); | |
107 } | |
108 } | |
109 | |
110 $form['nid'] = array( | |
111 '#type' => 'value', | |
112 '#value' => $conf['nid'], | |
113 ); | |
114 | |
115 $form['set_identifier'] = array( | |
116 '#type' => 'checkbox', | |
117 '#default_value' => FALSE, | |
118 '#title' => t('Reset identifier to node title'), | |
119 '#description' => t('If checked, the identifier will be reset to the node title of the selected node.'), | |
120 ); | |
121 | |
122 return $form; | |
123 } | |
124 | |
125 /** | |
126 * Validate a node. | |
127 */ | |
128 function ctools_context_node_edit_form_settings_form_validate($form, &$form_state) { | |
129 // Validate the autocomplete | |
130 if (empty($form_state['values']['nid']) && empty($form_state['values']['node'])) { | |
131 form_error($form['node'], t('You must select a node.')); | |
132 return; | |
133 } | |
134 | |
135 if (empty($form_state['values']['node'])) { | |
136 return; | |
137 } | |
138 | |
139 $nid = $form_state['values']['node']; | |
140 $preg_matches = array(); | |
141 $match = preg_match('/\[id: (\d+)\]/', $nid, $preg_matches); | |
142 if (!$match) { | |
143 $match = preg_match('/^id: (\d+)/', $nid, $preg_matches); | |
144 } | |
145 | |
146 if ($match) { | |
147 $nid = $preg_matches[1]; | |
148 } | |
149 if (is_numeric($nid)) { | |
150 $node = db_query('SELECT nid, status FROM {node} WHERE nid = :nid', array(':nid' => $nid))->fetchObject(); | |
151 } | |
152 else { | |
153 $node = db_query('SELECT nid, status FROM {node} WHERE LOWER(title) = LOWER(:title)', array(':title' => $nid))->fetchObject(); | |
154 } | |
155 | |
156 // Do not allow unpublished nodes to be selected by unprivileged users | |
157 if (!$node || (empty($node->status) && !(user_access('administer nodes')))) { | |
158 form_error($form['node'], t('Invalid node selected.')); | |
159 } | |
160 else { | |
161 form_set_value($form['nid'], $node->nid, $form_state); | |
162 } | |
163 } | |
164 | |
165 function ctools_context_node_edit_form_settings_form_submit($form, &$form_state) { | |
166 if ($form_state['values']['set_identifier']) { | |
167 $node = node_load($form_state['values']['nid']); | |
168 $form_state['values']['identifier'] = $node->title; | |
169 } | |
170 | |
171 // This will either be the value set previously or a value set by the | |
172 // validator. | |
173 $form_state['conf']['nid'] = $form_state['values']['nid']; | |
174 } | |
175 | |
176 /** | |
177 * Provide a list of ways that this context can be converted to a string. | |
178 */ | |
179 function ctools_context_node_edit_convert_list() { | |
180 // Pass through to the "node" context convert list. | |
181 $plugin = ctools_get_context('node'); | |
182 return ctools_context_node_convert_list(); | |
183 } | |
184 | |
185 /** | |
186 * Convert a context into a string. | |
187 */ | |
188 function ctools_context_node_edit_convert($context, $type) { | |
189 // Pass through to the "node" context convert list. | |
190 $plugin = ctools_get_context('node'); | |
191 return ctools_context_node_convert($context, $type); | |
192 } |