| [ Index ] |
PHP Cross Reference of Drupal 6 (gatewave) |
[Summary view] [Print] [Text view]
1 <?php 2 // $Id: form_builder_examples.module,v 1.10 2009/01/15 19:04:34 quicksketch Exp $ 3 4 /** 5 * @file form_builder_examples.module 6 * Sample implementations of form_builder. 7 */ 8 9 /** 10 * Implementation of hook_menu(). 11 */ 12 function form_builder_examples_menu() { 13 $items = array(); 14 15 $items['form-builder-example'] = array( 16 'title' => 'Form builder example', 17 'page callback' => 'form_builder_interface', 18 'page arguments' => array('example', 'sample'), 19 'file' => 'includes/form_builder.admin.inc', 20 'file path' => drupal_get_path('module', 'form_builder'), 21 'access arguments' => array('access content'), 22 'type' => MENU_NORMAL_ITEM, 23 ); 24 25 $items['form-builder-example/edit'] = array( 26 'title' => 'Edit', 27 'page callback' => 'form_builder_interface', 28 'page arguments' => array('example', 'sample'), 29 'file' => 'includes/form_builder.admin.inc', 30 'file path' => drupal_get_path('module', 'form_builder'), 31 'access arguments' => array('access content'), 32 'type' => MENU_DEFAULT_LOCAL_TASK, 33 ); 34 35 $items['form-builder-example/import'] = array( 36 'title' => 'Import', 37 'page callback' => 'drupal_get_form', 38 'page arguments' => array('form_builder_examples_import_form'), 39 // TODO: Make a form import. 40 'access callback' => FALSE, 41 'access arguments' => array('access content'), 42 'type' => MENU_LOCAL_TASK, 43 ); 44 45 $items['form-builder-example/export'] = array( 46 'title' => 'Export', 47 'page callback' => 'drupal_get_form', 48 'page arguments' => array('form_builder_examples_export_form'), 49 'access arguments' => array('access content'), 50 'type' => MENU_LOCAL_TASK, 51 ); 52 53 return $items; 54 } 55 56 /** 57 * A sample form array for testings. 58 */ 59 function form_builder_examples_example() { 60 $form = array( 61 'sample_textfield' => array( 62 '#form_builder' => array( 63 'element_id' => 'sample_textfield', 64 'element_type' => 'textfield', 65 'configurable' => TRUE, 66 'removable' => TRUE, 67 ), 68 '#type' => 'textfield', 69 '#title' => 'Sample textfield', 70 '#default_value' => 'a sample value', 71 '#field_prefix' => 'Prefix: ', 72 '#field_suffix' => ' :Suffix', 73 '#size' => 20, 74 '#weight' => 0, 75 ), 76 'sample_checkboxes' => array( 77 '#form_builder' => array( 78 'element_id' => 'sample_checkboxes', 79 'element_type' => 'checkboxes', 80 'configurable' => TRUE, 81 'removable' => TRUE, 82 ), 83 '#type' => 'checkboxes', 84 '#title' => 'Sample checkboxes', 85 '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), 86 '#default_value' => array('two'), 87 '#weight' => 1, 88 '#multiple' => 1, 89 ), 90 'sample_textarea' => array( 91 '#form_builder' => array( 92 'element_id' => 'sample_textarea', 93 'element_type' => 'textarea', 94 'configurable' => TRUE, 95 'removable' => TRUE, 96 ), 97 '#type' => 'textarea', 98 '#title' => 'Sample textarea', 99 '#default_value' => 'Text area sample value', 100 '#weight' => 2, 101 ), 102 'sample_radios' => array( 103 '#form_builder' => array( 104 'element_id' => 'sample_radios', 105 'element_type' => 'radios', 106 'configurable' => TRUE, 107 'removable' => TRUE, 108 ), 109 '#type' => 'radios', 110 '#title' => 'Sample radios', 111 '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), 112 '#default_value' => 'two', 113 '#weight' => 3, 114 ), 115 'sample_select' => array( 116 '#form_builder' => array( 117 'element_id' => 'sample_select', 118 'element_type' => 'select', 119 'configurable' => TRUE, 120 'removable' => TRUE, 121 ), 122 '#type' => 'select', 123 '#title' => 'Sample select', 124 '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), 125 '#default_value' => 'two', 126 '#weight' => 4, 127 '#multiple_toggle' => TRUE, 128 ), 129 ); 130 131 return $form; 132 } 133 134 /** 135 * Implementation of hook_form_builder_field_access(). 136 */ 137 function form_builder_examples_form_builder_field_access($op, $form_type, $form_id, $element) { 138 // If desiring to add extra access permissions return FALSE if access is denied. 139 // return user_access('administer content types'); 140 } 141 142 /** 143 * Implementation of hook_form_builder_types(). 144 * 145 * Define the supported fields and properties supported by CCK. 146 */ 147 function form_builder_examples_form_builder_types() { 148 $fields = array(); 149 150 $fields['fieldset'] = array( 151 'title' => t('Fieldset'), 152 'properties' => array( 153 'title', 154 'description', 155 'collapsible', 156 'collapsed', 157 ), 158 'default' => array( 159 '#title' => t('New fieldset'), 160 '#type' => 'fieldset', 161 '#collapsible' => TRUE, 162 '#collapsed' => FALSE, 163 ), 164 ); 165 166 $fields['number'] = array( 167 'title' => t('Number'), 168 'properties' => array( 169 'title', 170 'description', 171 'field_prefix', 172 'field_suffix', 173 'default_value', 174 'required', 175 ), 176 // TODO: how to handle validate functions? 177 'validate' => array( 178 'number', 179 ), 180 'default' => array( 181 '#key' => 'number', 182 '#title' => t('New number'), 183 '#type' => 'textfield', 184 ), 185 ); 186 187 $fields['select'] = array( 188 'title' => t('Select list'), 189 'properties' => array( 190 'title', 191 'description', 192 'default_value', 193 'required', 194 'options', 195 'multiple', // Handled by options element. 196 'key_type', // Handled by options element. 197 ), 198 'default' => array( 199 '#title' => t('New select list'), 200 '#type' => 'select', 201 '#options' => array('1' => 'one', '2' => 'two', '3' => 'three'), 202 '#multiple_toggle' => TRUE, 203 '#key_type' => 'associative', 204 ), 205 ); 206 207 // TODO: Merge checkbox with checkboxes? 208 /* 209 $fields['checkbox'] = array( 210 'title' => t('Checkbox'), 211 'properties' => array( 212 'title', 213 'description', 214 'return_value', 215 'default_value', 216 'required', 217 ), 218 'default' => array( 219 '#title' => t('New checkbox'), 220 '#type' => 'checkbox', 221 ), 222 ); 223 */ 224 225 $fields['checkboxes'] = array( 226 'title' => t('Checkboxes'), 227 'properties' => array( 228 'title', 229 'description', 230 'default_value', 231 'required', 232 'options', 233 'multiple', 234 'key_type', // Handled by options element. 235 ), 236 'default' => array( 237 '#title' => t('New checkboxes'), 238 '#type' => 'checkboxes', 239 '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), 240 ), 241 ); 242 243 $fields['radios'] = array( 244 'title' => t('Radios'), 245 'properties' => array( 246 'title', 247 'description', 248 'default_value', 249 'required', 250 'options', 251 'key_type', // Handled by options element. 252 ), 253 'default' => array( 254 '#title' => t('New radios'), 255 '#type' => 'radios', 256 '#options' => array('one' => 'one', 'two' => 'two', 'three' => 'three'), 257 ), 258 ); 259 260 $fields['textfield'] = array( 261 'title' => t('Textfield'), 262 'properties' => array( 263 'title', 264 'description', 265 'field_prefix', 266 'field_suffix', 267 'default_value', 268 'required', 269 'size', 270 ), 271 'default' => array( 272 '#title' => t('New textfield'), 273 '#type' => 'textfield', 274 ), 275 ); 276 277 $fields['textarea'] = array( 278 'title' => t('Textarea'), 279 'properties' => array( 280 'title', 281 'description', 282 'default_value', 283 'required', 284 'rows', 285 'cols', 286 'input_format', 287 ), 288 'default' => array( 289 '#title' => t('New textarea'), 290 '#type' => 'textarea', 291 ), 292 ); 293 294 $fields['file'] = array( 295 'title' => t('File'), 296 'properties' => array( 297 'title', 298 'description', 299 'required', 300 'size', 301 'file_extension_validate', 302 'file_size_validate', 303 ), 304 'default' => array( 305 '#title' => t('New file'), 306 '#type' => 'file', 307 '#size' => 30, 308 ), 309 ); 310 311 $fields['image'] = array( 312 'title' => t('Image'), 313 'properties' => array( 314 'title', 315 'description', 316 'required', 317 ), 318 'validate' => array( 319 'file_extension_validate', 320 'file_size_validate', 321 'file_image_size', 322 ), 323 'default' => array( 324 '#title' => t('New image'), 325 '#type' => 'file', 326 '#size' => 30, 327 ), 328 ); 329 330 return array( 331 'example' => $fields, 332 ); 333 } 334 335 /** 336 * Implementation of hook_form_builder_preview_alter(). 337 */ 338 function form_builder_examples_form_builder_preview_alter($element, $form_type, $form_id) { 339 if ($form_type == 'form_builder_examples') { 340 if (isset($element['#description'])) { 341 $element['#description'] = filter_xss($element['#description']); 342 } 343 } 344 } 345 346 /** 347 * Implementation of hook_form_builder_load(). 348 * 349 * Convert a CCK content type into a standard form builder array. 350 * 351 * @param $form_id 352 * The unique identifier for the form being edited. For CCK, this is the 353 * content type name. 354 */ 355 function form_builder_examples_form_builder_load($form_type, $form_id) { 356 if ($form_type == 'example') { 357 $form = form_builder_examples_example(); 358 359 return $form; 360 } 361 } 362 363 /** 364 * Implementation of hook_form_builder_save(). 365 * 366 * Take a form builder array and save it to the content type. 367 */ 368 function form_builder_examples_form_builder_save($form, $form_type, $form_id) { 369 if ($form_type == 'example') { 370 // Save the settings based on the changes to the $form array. 371 } 372 } 373 374 /** 375 * Menu callback for importing an entire FAPI array. 376 */ 377 function form_builder_examples_import_form(&$form_state) { 378 $form = array(); 379 380 $form['import'] = array( 381 '#type' => 'textarea', 382 '#title' => t('Import code'), 383 ); 384 385 return $form; 386 } 387 388 /** 389 * Menu callback for exporting an entire FAPI array. 390 */ 391 function form_builder_examples_export_form() { 392 module_load_include('inc', 'form_builder', 'includes/form_builder.api'); 393 module_load_include('inc', 'form_builder', 'includes/form_builder.cache'); 394 395 $form = array(); 396 397 $current = form_builder_cache_load('example', 'sample'); 398 399 $form['export'] = array( 400 '#type' => 'textarea', 401 '#title' => t('Export code'), 402 '#default_value' => form_builder_examples_export($current), 403 '#attributes' => array('readonly' => 'readonly', 'style' => 'font-family: monospace;'), 404 '#rows' => 20, 405 ); 406 407 return $form; 408 } 409 410 411 function form_builder_examples_export($form) { 412 $output = ''; 413 $output .= form_builder_examples_export_recurse($form); 414 $output .= 'return $form;'; 415 return $output; 416 } 417 418 /** 419 * Recursive function for pretty-printing of FAPI arrays. 420 */ 421 function form_builder_examples_export_recurse($form, $parents = array()) { 422 $output = ''; 423 424 // Sort this level of the array according to weight. 425 uasort($form, 'element_sort'); 426 427 // Print out this parent element and it's properties. 428 $properties = element_properties($form); 429 $omit = array('#form_builder', '#key'); 430 if (count($properties)) { 431 $output .= form_builder_examples_export_variable_name($parents) . " = array(\n"; 432 foreach (element_properties($form) as $property) { 433 if (!in_array($property, $omit)) { 434 if (is_array($form[$property])) { 435 $output .= " '". $property . "' => array(\n"; 436 foreach ($form[$property] as $key => $value) { 437 $output .= " '" . $key . "' => '". $value ."',\n"; 438 } 439 $output .= " ),\n"; 440 } 441 else { 442 $output .= " '". $property . "' => '" . $form[$property] ."',\n"; 443 } 444 } 445 } 446 $output .= ");\n"; 447 } 448 else { 449 $output .= form_builder_examples_export_variable_name($parents) . " = array();\n"; 450 } 451 452 foreach (element_children($form) as $key) { 453 $parents[] = $key; 454 $output .= form_builder_examples_export_recurse($form[$key], $parents); 455 array_pop($parents); 456 } 457 458 return $output; 459 } 460 461 462 function form_builder_examples_export_variable_name($parents) { 463 $output = '$form'; 464 foreach ($parents as $parent) { 465 $output .= "['". $parent ."']"; 466 } 467 return $output; 468 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Thu Mar 24 11:18:33 2011 | Cross-referenced by PHPXref 0.7 |