2 // $Id: content.node_form.inc,v 1.7.2.18 2009/02/10 22:53:04 yched Exp $
6 * Create fields' form for a content type.
8 * Each field defines its own component of the content entry form, via its
11 function content_form(&$form, &$form_state) {
12 $type = content_types($form['type']['#value']);
13 foreach ($type['fields'] as $field_name => $field) {
14 $form['#field_info'][$field['field_name']] = $field;
15 $form += (array) content_field_form($form, $form_state, $field);
21 * Create a separate form element for each field.
23 * // TODO: $count param ? not used anymore ?
24 * Hook_widget() picks up two new values, $count and $delta, to help
25 * widgets know what information to return since multiple values are
26 * sometimes controlled by the content module.
29 * the form to add this field element to
31 * the form_state for the above form
33 * the field array to use to create the form element
35 * use to get only a specific delta value of a multiple value field, otherwise
36 * function will return the entire $field form element.
38 function content_field_form(&$form, &$form_state, $field, $get_delta = NULL) {
39 $form['#cache'] = FALSE;
40 $node = $form['#node'];
42 $form_element = array();
43 $field_name = $field['field_name'];
45 // See if access to this form element is restricted,
46 // if so, skip widget processing and just set the value.
47 $access = content_access('edit', $field);
49 $addition[$field_name] = array(
52 '#value' => $node->$field_name,
57 // TODO: is the "if (function_exists($function)) {" needed ?
58 // defining the $function here makes it unclear where it is actually called
59 $function = $field['widget']['module'] .'_widget';
60 if (function_exists($function)) {
61 // Prepare the values to be filled in the widget.
62 // We look in the following places:
63 // - Form submitted values
64 // - Node values (when editing an existing node), or pre-filled values (when
65 // creating a new node translation)
66 // - Default values set for the field (when creating a new node).
68 if (!empty($form_state['values'][$field['field_name']])) {
69 $items = $form_state['values'][$field['field_name']];
70 // If there was an AHAH add more button in this field, don't save it.
71 unset($items[$field['field_name'] .'_add_more']);
73 elseif (!empty($node->$field['field_name'])) {
74 $items = $node->$field['field_name'];
76 elseif (empty($node->nid)) {
77 if (content_callback('widget', 'default value', $field) != CONTENT_CALLBACK_NONE) {
78 // If a module wants to insert custom default values here,
79 // it should provide a hook_default_value() function to call,
80 // otherwise the content module's content_default_value() function
82 $callback = content_callback('widget', 'default value', $field) == CONTENT_CALLBACK_CUSTOM ? $field['widget']['module'] .'_default_value' : 'content_default_value';
83 if (function_exists($callback)) {
84 $items = $callback($form, $form_state, $field, 0);
89 // If content module handles multiple values for this form element,
90 // and not selecting an individual $delta, process the multiple value form.
91 if (!isset($get_delta) && content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
92 $form_element = content_multiple_value_form($form, $form_state, $field, $items);
94 // If the widget is handling multiple values (e.g optionwidgets),
95 // or selecting an individual element, just get a single form
96 // element and make it the $delta value.
98 $delta = isset($get_delta) ? $get_delta : 0;
99 if ($element = $function($form, $form_state, $field, $items, $delta)) {
100 $title = check_plain(t($field['widget']['label']));
101 $description = content_filter_xss(t($field['widget']['description']));
103 '#required' => $get_delta > 0 ? FALSE : $field['required'],
104 '#columns' => array_keys($field['columns']),
106 '#description' => $description,
108 '#field_name' => $field['field_name'],
109 '#type_name' => $field['type_name'],
111 // If we're processing a specific delta value for a field where the
112 // content module handles multiples, set the delta in the result.
113 // For fields that handle their own processing, we can't make assumptions
114 // about how the field is structured, just merge in the returned value.
115 if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) {
116 $form_element[$delta] = array_merge($element, $defaults);
119 $form_element = array_merge($element, $defaults);
124 // Field name is needed at top level as well as the individual elements
125 // so the multiple values or other field level theme or processing can find it.
128 '#field_name' => $field['field_name'],
130 '#weight' => $field['widget']['weight'],
131 '#access' => $access,
132 // TODO: what's the need for #count ? does not seem to be used anywhere ?
133 '#count' => count($form_element),
135 $addition[$field['field_name']] = array_merge($form_element, $defaults);
142 * Special handling to create form elements for multiple values.
144 * Handles generic features for multiple fields:
145 * - number of widgets
146 * - AHAH-'add more' button
147 * - drag-n-drop value reordering
149 function content_multiple_value_form(&$form, &$form_state, $field, $items) {
150 $field_name = $field['field_name'];
152 switch ($field['multiple']) {
157 $filled_items = content_set_empty($field, $items);
158 $current_item_count = isset($form_state['item_count'][$field_name])
159 ? $form_state['item_count'][$field_name]
161 // We always want at least one empty icon for the user to fill in.
162 $max = ($current_item_count > count($filled_items))
163 ? $current_item_count - 1
164 : $current_item_count;
168 $max = $field['multiple'] - 1;
172 $title = check_plain(t($field['widget']['label']));
173 $description = content_filter_xss(t($field['widget']['description']));
175 $form_element = array(
176 '#theme' => 'content_multiple_values',
178 '#required' => $field['required'],
179 '#description' => $description,
181 $function = $field['widget']['module'] .'_widget';
183 for ($delta = 0; $delta <= $max; $delta++) {
184 if ($element = $function($form, $form_state, $field, $items, $delta)) {
186 '#title' => ($field['multiple'] >= 1) ? '' : $title,
187 '#description' => ($field['multiple'] >= 1) ? '' : $description,
188 '#required' => $delta == 0 && $field['required'],
191 '#columns' => array_keys($field['columns']),
192 '#field_name' => $field_name,
193 '#type_name' => $field['type_name'],
196 // Add an input field for the delta (drag-n-drop reordering), which will
197 // be hidden by tabledrag js behavior.
198 if ($field['multiple'] >= 1) {
199 // We name the element '_weight' to avoid clashing with column names
200 // defined by field modules.
201 $element['_weight'] = array(
203 '#delta' => $max, // this 'delta' is the 'weight' element's property
204 '#default_value' => isset($items[$delta]['_weight']) ? $items[$delta]['_weight'] : $delta,
209 $form_element[$delta] = array_merge($element, $defaults);
213 // Add AHAH add more button, if not working with a programmed form.
214 if ($field['multiple'] == 1 && empty($form['#programmed'])) {
215 // Make sure the form is cached so ahah can work.
216 $form['#cache'] = TRUE;
217 $content_type = content_types($field['type_name']);
218 $field_name_css = str_replace('_', '-', $field_name);
220 $form_element[$field_name .'_add_more'] = array(
222 '#name' => $field_name .'_add_more',
223 '#value' => t('Add another item'),
224 '#weight' => $field['widget']['weight'] + $max + 1,
225 // Submit callback for disabled JavaScript. drupal_get_form() might get
226 // the form from the cache, so we can't rely on content_form_alter()
227 // including this file. Therefore, call a proxy function to do this.
228 '#submit' => array('content_add_more_submit_proxy'),
230 'path' => 'content/js_add_more/'. $content_type['url_str'] .'/'. $field_name,
231 'wrapper' => $field_name_css .'-items',
232 'method' => 'replace',
235 // When JS is disabled, the content_add_more_submit handler will find
236 // the relevant field using these entries.
237 '#field_name' => $field_name,
238 '#type_name' => $field['type_name'],
241 // Add wrappers for the fields and 'more' button.
242 $form_element['#prefix'] = '<div id="'. $field_name_css .'-items">';
243 $form_element['#suffix'] = '</div>';
244 $form_element[$field_name .'_add_more']['#prefix'] = '<div class="content-add-more clear-block">';
245 $form_element[$field_name .'_add_more']['#suffix'] = '</div>';
247 return $form_element;
251 * Submit handler to add more choices to a content form. This handler is used when
252 * JavaScript is not available. It makes changes to the form state and the
253 * entire form is rebuilt during the page reload.
255 function content_add_more_submit($form, &$form_state) {
256 // Set the form to rebuild and run submit handlers.
257 node_form_submit_build_node($form, $form_state);
258 $field_name = $form_state['clicked_button']['#field_name'];
259 $type_name = $form_state['clicked_button']['#type_name'];
261 // Make the changes we want to the form state.
262 if ($form_state['values'][$field_name][$field_name .'_add_more']) {
263 $form_state['item_count'][$field_name] = count($form_state['values'][$field_name]);
269 * Menu callback for AHAH addition of new empty widgets.
271 function content_add_more_js($type_name_url, $field_name) {
272 $type = content_types($type_name_url);
273 $field = content_fields($field_name, $type['type']);
275 if (($field['multiple'] != 1) || empty($_POST['form_build_id'])) {
277 drupal_json(array('data' => ''));
281 // Retrieve the cached form.
282 $form_state = array('submitted' => FALSE);
283 $form_build_id = $_POST['form_build_id'];
284 $form = form_get_cache($form_build_id, $form_state);
286 // Invalid form_build_id.
287 drupal_json(array('data' => ''));
291 // We don't simply return a new empty widget to append to existing ones, because
292 // - ahah.js won't simply let us add a new row to a table
293 // - attaching the 'draggable' behavior won't be easy
294 // So we resort to rebuilding the whole table of widgets including the existing ones,
295 // which makes us jump through a few hoops.
297 // The form that we get from the cache is unbuilt. We need to build it so that
298 // _value callbacks can be executed and $form_state['values'] populated.
299 // We only want to affect $form_state['values'], not the $form itself
300 // (built forms aren't supposed to enter the cache) nor the rest of $form_data,
301 // so we use copies of $form and $form_data.
303 $form_state_copy = $form_state;
304 $form_copy['#post'] = array();
305 form_builder($_POST['form_id'], $form_copy, $form_state_copy);
306 // Just grab the data we need.
307 $form_state['values'] = $form_state_copy['values'];
308 // Reset cached ids, so that they don't affect the actual form we output.
309 form_clean_id(NULL, TRUE);
311 // Sort the $form_state['values'] we just built *and* the incoming $_POST data
312 // according to d-n-d reordering.
313 unset($form_state['values'][$field_name][$field['field_name'] .'_add_more']);
314 foreach ($_POST[$field_name] as $delta => $item) {
315 $form_state['values'][$field_name][$delta]['_weight'] = $item['_weight'];
317 $form_state['values'][$field_name] = _content_sort_items($field, $form_state['values'][$field_name]);
318 $_POST[$field_name] = _content_sort_items($field, $_POST[$field_name]);
320 // Build our new form element for the whole field, asking for one more element.
321 $form_state['item_count'] = array($field_name => count($_POST[$field_name]) + 1);
322 $form_element = content_field_form($form, $form_state, $field);
323 // Let other modules alter it.
324 drupal_alter('form', $form_element, array(), 'content_add_more_js');
326 // Add the new element at the right place in the (original, unbuilt) form.
327 if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type['type'], $field_name))) {
328 $form[$group_name][$field_name] = $form_element[$field_name];
331 $form[$field_name] = $form_element[$field_name];
334 // Save the new definition of the form.
335 $form_state['values'] = array();
336 form_set_cache($form_build_id, $form, $form_state);
338 // Build the new form against the incoming $_POST values so that we can
339 // render the new element.
340 $delta = max(array_keys($_POST[$field_name])) + 1;
341 $_POST[$field_name][$delta]['_weight'] = $delta;
342 $form_state = array('submitted' => FALSE);
345 '#programmed' => FALSE,
347 $form = form_builder($_POST['form_id'], $form, $form_state);
349 // Render the new output.
350 $field_form = (!empty($group_name)) ? $form[$group_name][$field_name] : $form[$field_name];
351 // We add a div around the new content to receive the ahah effect.
352 $field_form[$delta]['#prefix'] = '<div class="ahah-new-content">'. (isset($field_form[$delta]['#prefix']) ? $field_form[$delta]['#prefix'] : '');
353 $field_form[$delta]['#suffix'] = (isset($field_form[$delta]['#suffix']) ? $field_form[$delta]['#suffix'] : '') .'</div>';
354 // Prevent duplicate wrapper.
355 unset($field_form['#prefix'], $field_form['#suffix']);
357 // If a newly inserted widget contains AHAH behaviors, they normally won't
358 // work because AHAH doesn't know about those - it just attaches to the exact
359 // form elements that were initially specified in the Drupal.settings object.
360 // The new ones didn't exist then, so we need to update Drupal.settings
361 // by ourselves in order to let AHAH know about those new form elements.
362 $javascript = drupal_add_js(NULL, NULL);
363 $output_js = isset($javascript['setting']) ? '<script type="text/javascript">jQuery.extend(Drupal.settings, '. drupal_to_js(call_user_func_array('array_merge_recursive', $javascript['setting'])) .');</script>' : '';
365 $output = theme('status_messages') . drupal_render($field_form) . $output_js;
367 // Using drupal_json() breaks filefield's file upload, because the jQuery
368 // Form plugin handles file uploads in a way that is not compatible with
369 // 'text/javascript' response type.
370 $_GLOBALS['devel_shutdown'] = FALSE;
371 print drupal_to_js(array('status' => TRUE, 'data' => $output));