How to embed a node form with drupal 6.

Submitted by fago on Mon, 08/11/2008 - 15:51
Developers often want to embed a node form. So I'll try to work out, how this is done best in drupal 6. For 5.x I provided the subform element module, which was an easy way to do it. However recently I found out, that embedding fully working node forms in 6.x isn't so easy any more. The funky JS stuff of node forms, which relies on the #ahah form API property, fails if $form doesn't look that way it expects.

So how to embed a node form with working AHAH?

Fortunately, it's not that hard either. We must just ensure to keep the structure of $form as in the original form. So don't move the embedded form in $form['fieldgroup'] - or it's #ahah stuff gonna break. So let's show how to embed a node creation form.

Subform Element 1.2 released!

Submitted by fago on Sun, 04/08/2007 - 14:07
I'm happy to announce the Subform Element module 1.2. It's intended to make developers life easier. It allows you to reuse existing forms inside your own forms, e.g. just embed a fully working node form. The notable changes since the 1.0 release are some new form element properties, first of all the new data separation feature!

data separation

Without data separation the values of each form will be transmitted as usual in the $_POST array. So if both forms use a field with the same name, e.g. 'title' both will end up in $_POST['title'], so obviously the latter title will overwrite the first one. This was an issue if you tried to put two node forms into one using subform elements. Subform Element 1.2 has #data_separation turned on per default, which let's subform element prefix all the elements name of a subform. So the data ends up in e.g. $_POST[$subform_id]['title'], where $subform_id is the id of your subform. Then the data is separated and you can safely combine forms with overlapping element names. Have a look at this handbook page, it shows how easy it is now to merge two node forms into one.

subform customization

So now it's easy to reuse every form by using the subform element. What's missing is an easy way to customize the subform, without modifying the original form. For this subform element 1.2 comes with two new optional properties:
  • #subform_after_build: An optional array of after_build functions for the subform.
  • #extra_form: An optional array of further form elements that will be added to the subform.
So with an #after_build function for the subform you can easily alter the original subform, e.g. hide some buttons. The #extra_forms property allows you to add some further form elements to the subform.

pageroute 5.x 1.0 and the new subform element module

Submitted by fago on Thu, 03/29/2007 - 21:13
I'm happy to announce the pageroute 5.x 1.0 release. There were quite some changes since the first 5.x compatible release 0.9, so have a look at the release notes!. You might have noticed, that I've created a new project: Subform Element. It's an API module, so just install it if another module tells you to do so - or of course if you are a developer. In short it provides a new form element type, that can be used by other modules. This form element allows you to reuse existing forms inside your form! I had developed it for pageroute, so as an affect pageroute depends on this module now.

Pageroute Nodefamily

Furthermore I've started with another new module: Pageroute Nodefamily. It's still under development, but it provides already some useful functionality. Currently this module allows you to associate a pageroute with a content type. Then the associated pageroute will be used for editing nodes of this content type. So if this is useful for you, you can already use this module for that. In future this module will create node relations for all nodes created within the pageroute. This way it will build "nodefamilies", families of nodes. The node of the content type associated with the pageroute will be the head of the family, holding all nodes together. Once created, the nodefamily could be edited with the associated pageroute. That's the idea, hopefully I've the time to finish it soon...

subforms: easy form reusing

Submitted by fago on Tue, 03/06/2007 - 23:56
drupal 5.x allows one to programmatically submit forms. So wouldn't it be neat to reuse whole forms? This is what the subform form-element allows you to do. So you can build forms that reuse existing forms while you extend them with further form items. Note that form reusing means not only reusing the visual representation, but also the validation and submit logic. First I've implemented this for pageroute, as pageroute reuses a lot of existing forms. As it's also useful for other modules, I've created an own project for it. I had already a prototype for 4.7, but it had some drawbacks. The actual code for 5.x is much better, it handles validation and submitting of subforms correctly, so the subform will only be submitted if the main-form has also no validation errors. How to use it? All one needs to do, is to create a form element of the #type 'subform'. It needs the form_id as parameter (#id) and optionally it takes also #arguments, which will be passed to the form function. Then unfortunately one has to define an additional submit handler (subform_element_submit), which cares for submitting the subforms (if the validation was fine). Of course this handler could also be called from an already existing submit handler. ok, let's show a short usage example.
<?php function subform_test_form($node, $page) {
//this will present a full working node edit form
$form['node'] = array(
'#type' =?>
    '#id' =&gt; $node-&gt;type .'_node_form',
    '#arguments' =&gt; array($node),
  //this sets the subform submit handler
  $form['#submit']['subform_element_submit'] = array();

  return $form;
So this will create a fully working node form for you! Note: Now there is an own subform element project and a documentation page in the handbook, which contains another example.