Page tree
Skip to end of metadata
Go to start of metadata

This document describes how to capture data submitted with a form and how to store that data in the Data module. It just describes how to get the data. You still need to think of a business to do something useful with the data. The screenshots are taken from Magnolia 4.5 but the technique can be used with Magnolia 4.4 too.

Create a new data type

Create a new data type in the Data module so that you can store the form data. See Hierarchical types / data for detailed steps. The example below creates a new data type named "Formdata".

  • Name: formdata
  • Title: Formdata
  • Menu title: Form data
  • Sort by name: checked

Add a dialog field

Edit the dialog that the system created for the new data type automatically. Add an edit field datafield on the mainTab.

Register the data type with Magnolia CMS

In you project module, add a task for installing the data type your created above.

Read the documentation about Creating a type in the Data module documentation, particularly the tip about RegisterNodeTypeTask.

FormDataVersionHandler
package info.magnolia.testmodule.setup;
import info.magnolia.module.DefaultModuleVersionHandler;
import info.magnolia.module.InstallContext;
import info.magnolia.module.delta.Task;
import info.magnolia.module.data.setup.RegisterNodeTypeTask;

import java.util.ArrayList;
import java.util.List;
/**
 * Version handler for test module.
 */
public class FormDataVersionHandler extends DefaultModuleVersionHandler {


    @Override
    protected List<Task> getBasicInstallTasks(InstallContext installContext) {
        final List<Task> installTasks = new ArrayList<Task>();

        installTasks.add(new RegisterNodeTypeTask("formdata"));

        installTasks.addAll(super.getBasicInstallTasks(installContext));
        return installTasks;
    }
}

Define a new form processor

Define a new form processor in the configuration of the Form module or preferably in your own form component definition. See Form processors for instructions.

Create your own form component definition

If different types of form behavior are required in your project, for example different processes should be triggered after submitting different types of forms, then create your own form component definition rather than adding it to the default form component, and attach the form processor to it. This is a very common scenario. For example, if you have a contact form on the site you may want to send the submitted data in email instead of storing it in the Data module. In such a case you want to create a separate form component definition. Remember that you can extend configuration so there is no need to duplicate the whole definition!

This is example code for a custom form processor saving data using Magnolia 4.5:

SaveDataFormProcessor for 4.5
package info.magnolia.module.form.processors;

import info.magnolia.context.MgnlContext;
import info.magnolia.jcr.util.NodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.util.Calendar;
import java.util.Map;
public class SaveDataFormProcessor extends AbstractFormProcessor {
    private static final Logger log = LoggerFactory.getLogger(SaveDataFormProcessor.class);
    @Override
    protected void internalProcess(Node content, Map<String, Object> parameters) throws FormProcessorFailedException {
        try {
            Session session = MgnlContext.getJCRSession("data");
            Node dataNode = NodeUtil.createPath(session.getRootNode(), "/formdata/" + parameters.get("name"), "formdata");
            dataNode.setProperty("name", parameters.get("name").toString());
            dataNode.setProperty("datafield", parameters.get("datafield").toString());
            dataNode.setProperty("dateCreated", Calendar.getInstance());
            session.save();
        } catch (RepositoryException e) {
            log.error("Exception Handling...: " + e.getMessage());
        }
    }   
}

For Magnolia 4.4:

SaveDataFormProcessor for 4.4
package info.magnolia.module.form.processors;

import info.magnolia.cms.core.Content;
import info.magnolia.cms.core.HierarchyManager;
import info.magnolia.cms.core.ItemType;
import info.magnolia.cms.util.ContentUtil;
import info.magnolia.context.MgnlContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import java.util.Calendar;
import java.util.Map;
public class SaveDataFormProcessor extends AbstractFormProcessor {
    private static final Logger log = LoggerFactory.getLogger(SaveDataFormProcessor.class);
    @Override
    protected void internalProcess(Node content, Map<String, Object> parameters) throws FormProcessorFailedException {
        HierarchyManager hm = MgnlContext.getHierarchyManager("data");
        try {
            Content formdata = hm.getContent("/formdata");
            // Registered data type
            ItemType itemType = new ItemType("formdata");
            // Name of the data item
            String contentPath = "/formdata/" + parameters.get("name");
            // create item
            Content data = ContentUtil.createPath(hm, contentPath, itemType);
            // set attributes
            data.setNodeData("name", parameters.get("name").toString());
            data.setNodeData("datafield", parameters.get("datafield").toString());
            data.setNodeData("dateCreated", Calendar.getInstance());
            if (formdata != null) {
                try {
                    formdata.save();
                } catch (RepositoryException e) {
                    log.error("Save Repo: " + e.getMessage());
                }
            }
        } catch (RepositoryException e) {
            log.error("Exception: " + e.getMessage());
        }
    }
}

Create a form

Using the Form module create a form containing the fields you want to store. Create only fields that the user has to fill. You have the option to add more attributes in the code.

After when the form is submitted the data is available in the Data module.