logo

MMBase tips

A dynamic optionlist in the editwizards

tip #18 / Sun 1 May 2011 / André / 0 comments

The infamous EditWizards are used as editors in a lot of mmbase installations. Developed almost 10 years ago by for the VPRO they've evolved in very sophisticated editors. This example shows the creation of a dynamically filled options list including nodes from another nodetype.

A 'traditional' non-dynamic optionlist can be made like the following example. Values are hard coded into a list to create a pulldown.
Let's presume you have an editwizard for news articles and you want your editors to choose from a set of 'pools' nodes (you use them as topics or categories) to have them categorize the articles. With a non-dynamic list you have to asign aliases to all pools nodes and order them into a optionlist and include it in the list element of your wizard.

<list>
<optionlist name="categories_list">
   <option id="pool_general">General</option>
   <option id="pool_sports">Sports</option>
   <option id="pool_gossip">Gossip</option>
</optionlist>
</list>
A few lines lower in the wizard you use the defined 'pools_list' as follows (a bit arbitrary) in the subtitle field of your article. These lines generate a pull down with your categories.
<field name="subtitle" ftype="enum">
<prompt>Category</prompt>
<optionlist select="categories_list"></optionlist>
</field>

That results in a pulldown that will look more or less like this in plain html:

<select name="field/f_1/d_3" >
<option value="">Select...</option>
<option value="pool_general">General</option>
<option value="pool_sports">Sports</option>
<option value="pool_gossip">Gossip</option>
</select>

The same list made dynamic

With a bit of xpath magic the needed pools nodes you use as categories can be extracted dynamicaly from mmbase.

<lists>
  <optionlist name="categories_list"
    optionid="field[@name='number']"
    optioncontent="field[@name='name']">
    <query xpath="/*@pools" />
  </optionlist>
</lists>

The names of your category or pools nodes are displayed as labels of your options, their nodenumbers become the values. The xpath query <query xpath="/*@pools" /> queries mmbase for all pools nodes. You include 'categories_list' the same way as we did in the previous example.

Here is an example of a simple but complete editwizard for a typical mmbase news node that uses it:

<?xml version="1.0"?>
<!DOCTYPE wizard-schema PUBLIC "-//MMBase/DTD editwizard 1.0//EN"
"http://www.mmbase.org/dtd/wizard-schema_1_0.dtd">
<wizard-schema>
<title>News</title>
<action type="create">
  <object type="news" />
</action>
<action type="load" />

<lists>
 <!-- create the list with all pools nodes to use as categories -->
 <optionlist name="categories_list"
  optionid="field[@name='number']"
  optioncontent="field[@name='name']">
  <query xpath="/*@pools" />
 </optionlist>
</lists>

<steps>
  <step form-schema="step1" />
</steps>

<form-schema id="step1">
  <title>News</title>

<field name="title" ftype="line">
  <prompt>Title</prompt>
</field>

<!-- subtitle is misused to contain a category -->
<field name="subtitle" ftype="enum">
  <prompt>Subtitle</prompt>
  <optionlist select="categories_list"></optionlist>
</field>

<field name="intro" ftype="text">
  <prompt>Intro</prompt>
</field>

<field name="body" ftype="text">
  <prompt>Tekst</prompt>
</field>

</form-schema>
</wizard-schema>

0 comments

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm