<< ALL BLOG POSTS

Allow users to create content using PloneFormGen

Table of Contents

Let's pretend you've got a website featuring press releases about science and technology. You'd like to let your visitors submit their own press releases without having a site login. You're in luck! In this article, we'll cover two ways to achieve this task using PloneFormGen.

Site content can be created via PloneFormGen with a Custom Script Adapter or by using an add-on adapter called PloneFormGen Save Data to Content (uwosh.pfg.d2c). Additionally, you could write Python scripts to handle the submissions, but we will not be covering that method.

Creating Content using a Custom Script Adapter

1. Add and publish a new Folder to the root of your site called "User Content." This is where the new content will be saved.

2. Add a FormFolder, titled "Press Releases" and uncheck "Mailer" for the Action Adapter. Save.

3. Delete the default fields (comment, etc.) and add two required fields:

  • String Field: Press Release Title
  • Text Field: Press Release Content

4. Add a new Custom Script Adapter to the FormFolder via the Add New menu:

  • Set the Proxy Role to "Manager" (note the security risk here -- make sure your form does what you think it does and nothing else!)
  • Create the script which will create the new content. For our example, we're creating a Page (Document) in the site:
# Target folder is the newly-created Press Release folder
from Products.CMFCore.utils import getToolByName
urltool = getToolByName(context, "portal_url")
portal  = urltool.getPortalObject()
targetdir = getattr(portal, 'user-content')

# The form being submitted so we can access its contents
form = request.form

# Create a unique ID
from DateTime import DateTime
uid = str(DateTime().millis())

# Create a new Page (Document)
targetdir.invokeFactory("Document", id=uid, title=form['press-release-title'])

# Set the reference for our new Page
doc = targetdir[uid]

# Set the values for the content of the Page; use the short names of each item in the form
# (which you can see in the URL if you are viewing one of the fields directly)
doc.setFormat('text/plain')
doc.setText(form['press-release-content'])

# Reindexed the Page in the site
doc.reindexObject()

The item will be in the Private state by default, so you can review what was submitted. If you wanted to submit it for publication so that the state is Pending Review, append the following to the script:

# Submit for Review
portal_workflow = getToolByName(context, 'portal_workflow')
portal_workflow.doActionFor(doc, 'submit')

Or if you want it to go straight to Published (risky!), simply replace 'submit' with 'publish'.

Creating Content using the Save Data Adapter

If you have the ability to install add-on products on your site, another method of creating content using PloneFormGen is to use the PloneFormGen Save Data to Content adapter (uwosh.pfg.d2c).

First, we'll need to set up the content type we want to create in the ZMI. Using the same example as above, let's say we want to create a Press Release on the site. This adapter requires the content type to have 'uwosh.pfg.d2c' configured as its 'product'. This can be achieved in one of three ways: adding it programmatically, with genericsetup profile, or copying the existing one in portal_types tool. We'll use the last method in this example.

To create the type:

1. In your site as admin, go to /portal_types/manage_main

2. Clone the content type FormSaveData2ContentEntry by using copy and paste.

3. Check the box next to your new copy and select Rename. For our example, rename it to "Press Release."

Rename

4. Click on the type's title (now "Press Release") to edit additional fields:

  • Change the Title to "Press Release".
  • Change the Description to something informative such as "A document used for Press Releases" (or you can delete the default text and leave it blank).
  • Since we'd like the Press Release to visually appear like a Document (Page) in the site, change the value for the following fields to document_view:
    • Initial view name
    • Default view method
    • Available view methods
  • If you would also like your type to be addable without the use of the PFG form (e.g., using the Add New menu by users with permission), check the box for Implicitly Addable.
Edit Type

 

 

To create the Form used by visitors to submit a Press Release:

 1. Add a FormFolder, titled "Press Releases" and uncheck "Mailer" for the Action Adapter. Save.

2. Delete the default fields (comment, etc.) and add two required fields:

  • String Field: Press Release Title
  • Text Field: Press Release Content

3. Because the document_view template used to display the Press Release expects the body content to actually be referred to as the name "text," we need to change the short name of the Press Release Content field:

  • View the folder_contents of the form
  • Check the box next to "Press Release Content" and click "rename"
  • Change the "Short Name" to "text"

4. Add a new Save Data to Content Adapter to the FormFolder via the Add New menu:

  • Title: Create Press Release
  • Title Field: press-release-title
  • Saved Entry Content-Type: Press Release

You can also choose which workflow to apply to a Press Release at this time.

Create

 5. Publish your form.

Anonymous visitors to the site may now submit Press Releases without needing an account to log in to on your site.

The Press Releases will be created inside the Content Adapter (it is folder-ish), and will be private by default unless you selected a workflow which allows the item to be automatically published. Because the Content Adapter is still private, these items will not be available via search. Publish and move approved items to a published folder location to have them searchable and visible to users on your site.

The layout view applied (document_view) will be the same as a Document (Page), so the title will appear in the title location and the body will display the content. If we were to just use the default base_view when creating the new Press Release content type, it would instead display the field names as captions for each item.

I hope this article helped give a basic understanding of how to allow your users to create site content with PloneFormGen. Have questions or comments? Sound off in the comments section below and be sure to sign up for our Plone & Python How-To digests to receive more how-to guides as soon as they are published!
Related Posts
How can we assist you?
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.