[drupal-devel] Rewriting use of forms in Drupal

Gerhard Killesreiter killesreiter at physik.uni-freiburg.de
Fri Jun 3 15:22:43 UTC 2005



On Fri, 3 Jun 2005, Adrian Rossouw wrote:

> -----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'));

I'd rewrite the last part to

 $elements['group1'] = array('type' => 'group', '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);

And then for elements:
      $elements['element1'] =  array('type'=>'textfield',
 'title'=> t('element 1'), 'value' => $_POST['edit']['element1'],
  'default' => $obj->element1, 'weight' => 1, 'group' => 'group1');

This avoids nested arrays which is a good thing.

>      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);
>    }
> }

Looks good.

> 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;
> }

My change above makes this code simpler too.

> 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 would take care of David's problem.

> 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);
> }

I am not sure we should allow modules to change form types.

> 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.

I think I solved this problem, too.

Cheers,
	Gerhard



More information about the drupal-devel mailing list