Tuesday, July 31, 2012

Use alfresco forms framework for categories

I wanted to use the forms framework in alfresco for creating new categories ... works fine until you save the changes and the TypeFormProcessor handles the association to the parent (defaults to cm:contains like for folders).

So you need a custom FormProcessor, like:

public class CategoryFormProcessor extends TypeFormProcessor { ...

You need to override the creation of a node:

protected NodeRef createNode(TypeDefinition typeDef, FormData data) {  ...

Take the code from TypeFormProcessor and change the Association saved to ContentModel.ASSOC_SUBCATEGORIES approx. line 107.

Then you must configure you bean in the Repo context (some loaded
*-context.xml file), see alfrescos in form-services-context.xml

<bean id="categoryTypeFormProcessor"     class="com.redpill_linpro.alfresco.noark5.repo.forms.processor.node.CategoryFormProcessor"
      parent="typeFormProcessor">
        <property name="filterRegistry" ref="typeFilterRegistry" />
      <property name="matchPattern">
         <value>category-type</value>
      </property>
   </bean>


NB! the code (class file for the new FormProcessor) must be in the WEB-INF (not share), because of class-loading dependencies.

No you can load a form, example javascript snipplet here with a configured destination.

var templateUrl = YAHOO.lang.substitute(Alfresco.constants.URL_SERVICECONTEXT + "components/form?itemKind={itemKind}&itemId={itemId}&destination={destination}&mode={mode}&submitType={submitType}&formId={formId}&showCancelButton=true",
             {
                itemKind: "category-type",
                itemId: "cm:category",
                destination: destination,
                mode: "create",
                submitType: "json",
                formId: "doclib-common"
             });

             // Using Forms Service, so always create new instance
             var createFolder = new Alfresco.module.SimpleDialog(this.id + "-createCategory");

             createFolder.setOptions(
             {
                width: "33em",
                templateUrl: templateUrl,

...

The itemKind variable trickers the formprocessor! type->TypeFormProcessor, category-type -> CategoryTypeFormProcessor, which I configured in the bean ...

This works for form type 'create' and sub-categories ... I will have to investigate more ... but it is nice to be able to reuse the form-framework/knowledge for categories instead of rolling your own ... this also supports custom categories, children / inherited types of cm:category.