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