[drupal-devel] Rewriting use of forms in Drupal
Adrian Rossouw
adrian at bryght.com
Fri Jun 3 15:07:55 UTC 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Here's what I am envisioning :
/* This is the entire form definition */
function somemodule_formname($obj) {
$elements['title'] = array('type'=>'textfield', 'title'=> t
('Title'), $value= $_POST['edit']['title'], 'default' => $obj->title,
'weight' => 0);
$elements['group1'] = array('weight' => 1, 'title' => t('some
title here'));
$elements['group1']['element1'] = array('type'=>'textfield',
'title'=> t('element 1'), $value= $_POST['edit']['group1']
['element1'], 'default' => $obj->element1, 'weight' => 0);
$elements['group1']['element1'] = array('type'=>'textfield',
'title'=> t('element 1'), $value= $_POST['edit']['group1']
['element1'], 'default' => $obj->element2, 'weight' => 1);
return form('somemodule_form', $elements);
}
/* this goes in common.inc */
function form($name, $elements) {
if ($form = theme('somemodule_formname', $elements)) {
return $form;
}
else {
return form_default_renderer($elements);
}
}
function form_default_renderer($elements) {
/* Sort elements via weight */
foreach (array_keys($elements) as $key) {
$form .= form_element($key);
}
return $form;
}
function form_element($element) {
if (/*test for group here*/) {
$form = form_group(form_default_renderer($element), $element
['title']);
$form = form_group($group, $element['title']);
}
else {
$form = ${'form_' . $element->type}($element); // Redirects
to the type specific element could also be a switch inside form_element
}
return $form;
}
What could then happen, is the module could define it's own form
layout, by doing :
function theme_modulename_formname($elements) {
$form .= form_element($element['title']);
$form .= form_element($element['group1']); /* renders entire
group */
return $form;
}
This could of course be overridden.
Say you didn't want to display the title , you could do a
function mytheme_modulename_formname($elements) {
$elements['title']['type'] => 'hidden';
return form_default_renderer($elements);
}
The biggest problem with this is the use of 'title' and 'weight' and
'type' and the like, since you could
have an element which is called 'type' , so what I would do is use
constants for the keys .. ie
TYPE = 'group' , TITLE => 'title goes here' etc.
- --
Adrian Rossouw
Drupal developer and Bryght Guy
http://drupal.org | http://bryght.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)
iD8DBQFCoHKqgegMqdGlkasRAhCAAJ9VA18IQxZTxyKFehy4hY5eC5jtmQCfXbYU
rf42MWxR7sXbddx72gNmlSE=
=Snue
-----END PGP SIGNATURE-----
More information about the drupal-devel
mailing list