| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: content.node_form.inc,v 1.7.2.24 2010/12/30 13:55:30 yched Exp $ 3 4 /** 5 * @file 6 * Create fields' form for a content type. 7 * 8 * Each field defines its own component of the content entry form, via its 9 * chosen widget. 10 */ 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); 16 } 17 return $form; 18 } 19 20 /** 21 * Create a separate form element for each field. 22 * 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. 27 * 28 * @param $form 29 * the form to add this field element to 30 * @param $form_state 31 * the form_state for the above form 32 * @param $field 33 * the field array to use to create the form element 34 * @param $get_delta 35 * use to get only a specific delta value of a multiple value field, otherwise 36 * function will return the entire $field form element. 37 */ 38 function content_field_form(&$form, &$form_state, $field, $get_delta = NULL) { 39 $form['#cache'] = FALSE; 40 $node = $form['#node']; 41 $addition = array(); 42 $form_element = array(); 43 $field_name = $field['field_name']; 44 45 $items = array(); 46 47 // TODO: is the "if (function_exists($function)) {" needed ? 48 // defining the $function here makes it unclear where it is actually called 49 $function = $field['widget']['module'] .'_widget'; 50 if (function_exists($function)) { 51 // Prepare the values to be filled in the widget. 52 // We look in the following places: 53 // - Form submitted values 54 // - Node values (when editing an existing node), or pre-filled values (when 55 // creating a new node translation) 56 // - Default values set for the field (when creating a new node). 57 if (!empty($form_state['values'][$field['field_name']])) { 58 $items = $form_state['values'][$field['field_name']]; 59 // If there was an AHAH add more button in this field, don't save it. 60 unset($items[$field['field_name'] .'_add_more']); 61 } 62 elseif (!empty($node->$field['field_name'])) { 63 $items = $node->$field['field_name']; 64 } 65 elseif (empty($node->nid)) { 66 if (content_callback('widget', 'default value', $field) != CONTENT_CALLBACK_NONE) { 67 // If a module wants to insert custom default values here, 68 // it should provide a hook_default_value() function to call, 69 // otherwise the content module's content_default_value() function 70 // will be used. 71 $callback = content_callback('widget', 'default value', $field) == CONTENT_CALLBACK_CUSTOM ? $field['widget']['module'] .'_default_value' : 'content_default_value'; 72 if (function_exists($callback)) { 73 $items = $callback($form, $form_state, $field, 0); 74 } 75 } 76 } 77 78 // See if access to this form element is restricted, 79 // if so, skip widget processing and just set the value. 80 $access = content_access('edit', $field, NULL, $node); 81 if (!$access) { 82 $addition[$field_name] = array( 83 '#access' => $access, 84 '#type' => 'value', 85 '#value' => $items, 86 ); 87 return $addition; 88 } 89 90 // If content module handles multiple values for this form element, 91 // and not selecting an individual $delta, process the multiple value form. 92 if (!isset($get_delta) && content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) { 93 $form_element = content_multiple_value_form($form, $form_state, $field, $items); 94 } 95 // If the widget is handling multiple values (e.g optionwidgets), 96 // or selecting an individual element, just get a single form 97 // element and make it the $delta value. 98 else { 99 $delta = isset($get_delta) ? $get_delta : 0; 100 if ($element = $function($form, $form_state, $field, $items, $delta)) { 101 $title = check_plain(t($field['widget']['label'])); 102 $description = content_filter_xss(t($field['widget']['description'])); 103 $defaults = array( 104 '#required' => $get_delta > 0 ? FALSE : $field['required'], 105 '#columns' => array_keys($field['columns']), 106 '#title' => $title, 107 '#description' => $description, 108 '#delta' => $delta, 109 '#field_name' => $field['field_name'], 110 '#type_name' => $field['type_name'], 111 ); 112 // If we're processing a specific delta value for a field where the 113 // content module handles multiples, set the delta in the result. 114 // For fields that handle their own processing, we can't make assumptions 115 // about how the field is structured, just merge in the returned value. 116 if (content_handle('widget', 'multiple values', $field) == CONTENT_HANDLE_CORE) { 117 $form_element[$delta] = array_merge($element, $defaults); 118 } 119 else { 120 $form_element = array_merge($element, $defaults); 121 } 122 } 123 } 124 125 // Field name is needed at top level as well as the individual elements 126 // so the multiple values or other field level theme or processing can find it. 127 if ($form_element) { 128 $defaults = array( 129 '#field_name' => $field['field_name'], 130 '#tree' => TRUE, 131 '#weight' => $field['widget']['weight'], 132 '#access' => $access, 133 // TODO: what's the need for #count ? does not seem to be used anywhere ? 134 '#count' => count($form_element), 135 ); 136 $addition[$field['field_name']] = array_merge($form_element, $defaults); 137 } 138 } 139 return $addition; 140 } 141 142 /** 143 * Special handling to create form elements for multiple values. 144 * 145 * Handles generic features for multiple fields: 146 * - number of widgets 147 * - AHAH-'add more' button 148 * - drag-n-drop value reordering 149 */ 150 function content_multiple_value_form(&$form, &$form_state, $field, $items) { 151 $field_name = $field['field_name']; 152 153 switch ($field['multiple']) { 154 case 0: 155 $max = 0; 156 break; 157 case 1: 158 $filled_items = content_set_empty($field, $items); 159 $current_item_count = isset($form_state['item_count'][$field_name]) 160 ? $form_state['item_count'][$field_name] 161 : count($items); 162 // We always want at least one empty icon for the user to fill in. 163 $max = ($current_item_count > count($filled_items)) 164 ? $current_item_count - 1 165 : $current_item_count; 166 167 break; 168 default: 169 $max = $field['multiple'] - 1; 170 break; 171 } 172 173 $title = check_plain(t($field['widget']['label'])); 174 $description = content_filter_xss(t($field['widget']['description'])); 175 176 $form_element = array( 177 '#theme' => 'content_multiple_values', 178 '#title' => $title, 179 '#required' => $field['required'], 180 '#description' => $description, 181 ); 182 $function = $field['widget']['module'] .'_widget'; 183 184 for ($delta = 0; $delta <= $max; $delta++) { 185 if ($element = $function($form, $form_state, $field, $items, $delta)) { 186 $defaults = array( 187 '#title' => ($field['multiple'] >= 1) ? '' : $title, 188 '#description' => ($field['multiple'] >= 1) ? '' : $description, 189 '#required' => $delta == 0 && $field['required'], 190 '#weight' => $delta, 191 '#delta' => $delta, 192 '#columns' => array_keys($field['columns']), 193 '#field_name' => $field_name, 194 '#type_name' => $field['type_name'], 195 ); 196 197 // Add an input field for the delta (drag-n-drop reordering), which will 198 // be hidden by tabledrag js behavior. 199 if ($field['multiple'] >= 1) { 200 // We name the element '_weight' to avoid clashing with column names 201 // defined by field modules. 202 $element['_weight'] = array( 203 '#type' => 'weight', 204 '#delta' => $max, // this 'delta' is the 'weight' element's property 205 '#default_value' => isset($items[$delta]['_weight']) ? $items[$delta]['_weight'] : $delta, 206 '#weight' => 100, 207 ); 208 } 209 210 $form_element[$delta] = array_merge($element, $defaults); 211 } 212 } 213 214 // Add AHAH add more button, if not working with a programmed form. 215 if ($field['multiple'] == 1 && empty($form['#programmed'])) { 216 // Make sure the form is cached so ahah can work. 217 $form['#cache'] = TRUE; 218 $content_type = content_types($field['type_name']); 219 $field_name_css = str_replace('_', '-', $field_name); 220 221 $form_element[$field_name .'_add_more'] = array( 222 '#type' => 'submit', 223 '#name' => $field_name .'_add_more', 224 '#value' => t('Add another item'), 225 '#weight' => $field['widget']['weight'] + $max + 1, 226 // Submit callback for disabled JavaScript. drupal_get_form() might get 227 // the form from the cache, so we can't rely on content_form_alter() 228 // including this file. Therefore, call a proxy function to do this. 229 '#submit' => array('content_add_more_submit_proxy'), 230 '#ahah' => array( 231 'path' => 'content/js_add_more/'. $content_type['url_str'] .'/'. $field_name, 232 'wrapper' => $field_name_css .'-items', 233 'method' => 'replace', 234 'effect' => 'fade', 235 ), 236 // When JS is disabled, the content_add_more_submit handler will find 237 // the relevant field using these entries. 238 '#field_name' => $field_name, 239 '#type_name' => $field['type_name'], 240 ); 241 242 // Add wrappers for the fields and 'more' button. 243 $form_element['#prefix'] = '<div id="'. $field_name_css .'-items">'; 244 $form_element['#suffix'] = '</div>'; 245 $form_element[$field_name .'_add_more']['#prefix'] = '<div class="content-add-more clear-block">'; 246 $form_element[$field_name .'_add_more']['#suffix'] = '</div>'; 247 } 248 return $form_element; 249 } 250 251 /** 252 * Submit handler to add more choices to a content form. This handler is used when 253 * JavaScript is not available. It makes changes to the form state and the 254 * entire form is rebuilt during the page reload. 255 */ 256 function content_add_more_submit($form, &$form_state) { 257 // Set the form to rebuild and run submit handlers. 258 node_form_submit_build_node($form, $form_state); 259 $field_name = $form_state['clicked_button']['#field_name']; 260 $type_name = $form_state['clicked_button']['#type_name']; 261 262 // Make the changes we want to the form state. 263 if ($form_state['values'][$field_name][$field_name .'_add_more']) { 264 $form_state['item_count'][$field_name] = count($form_state['values'][$field_name]); 265 } 266 } 267 268 /** 269 * Menu callback for AHAH addition of new empty widgets. 270 */ 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']); 274 275 if (($field['multiple'] != 1) || empty($_POST['form_build_id'])) { 276 // Invalid request. 277 drupal_json(array('data' => '')); 278 exit; 279 } 280 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); 285 if (!$form) { 286 // Invalid form_build_id. 287 drupal_json(array('data' => '')); 288 exit; 289 } 290 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. 296 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. 302 $form_copy = $form; 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); 310 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']; 316 } 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]); 319 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 // We pass an empty array as hook_form_alter's usual 'form_state' parameter, 325 // instead of $form_state (for reasons we may never remember). 326 // However, this argument is still expected to be passed by-reference 327 // (and PHP5.3 will throw an error if it isn't.) This leads to: 328 $data = &$form_element; 329 $empty_form_state = array(); 330 $data['__drupal_alter_by_ref'] = array(&$empty_form_state); 331 drupal_alter('form', $data, 'content_add_more_js'); 332 333 // Add the new element at the right place in the (original, unbuilt) form. 334 if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type['type'], $field_name))) { 335 $form[$group_name][$field_name] = $form_element[$field_name]; 336 } 337 else { 338 $form[$field_name] = $form_element[$field_name]; 339 } 340 341 // Save the new definition of the form. 342 $form_state['values'] = array(); 343 form_set_cache($form_build_id, $form, $form_state); 344 345 // Build the new form against the incoming $_POST values so that we can 346 // render the new element. 347 $delta = max(array_keys($_POST[$field_name])) + 1; 348 $_POST[$field_name][$delta]['_weight'] = $delta; 349 $form_state = array('submitted' => FALSE); 350 $form += array( 351 '#post' => $_POST, 352 '#programmed' => FALSE, 353 ); 354 $form = form_builder($_POST['form_id'], $form, $form_state); 355 356 // Render the new output. 357 $field_form = (!empty($group_name)) ? $form[$group_name][$field_name] : $form[$field_name]; 358 // We add a div around the new content to receive the ahah effect. 359 $field_form[$delta]['#prefix'] = '<div class="ahah-new-content">'. (isset($field_form[$delta]['#prefix']) ? $field_form[$delta]['#prefix'] : ''); 360 $field_form[$delta]['#suffix'] = (isset($field_form[$delta]['#suffix']) ? $field_form[$delta]['#suffix'] : '') .'</div>'; 361 // Prevent duplicate wrapper. 362 unset($field_form['#prefix'], $field_form['#suffix']); 363 364 // If a newly inserted widget contains AHAH behaviors, they normally won't 365 // work because AHAH doesn't know about those - it just attaches to the exact 366 // form elements that were initially specified in the Drupal.settings object. 367 // The new ones didn't exist then, so we need to update Drupal.settings 368 // by ourselves in order to let AHAH know about those new form elements. 369 $javascript = drupal_add_js(NULL, NULL); 370 $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>' : ''; 371 372 $output = theme('status_messages') . drupal_render($field_form) . $output_js; 373 374 // Using drupal_json() breaks filefield's file upload, because the jQuery 375 // Form plugin handles file uploads in a way that is not compatible with 376 // 'text/javascript' response type. 377 $GLOBALS['devel_shutdown'] = FALSE; 378 print drupal_to_js(array('status' => TRUE, 'data' => $output)); 379 exit; 380 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |