You are here: Home / Documentation / How To's / How to create a information portlet from a form

How to create a information portlet from a form

by ledwell published Oct 29, 2012 10:50 AM, last modified Aug 16, 2016 10:50 AM
Using PloneFormGen and D2C, create a portlet that will display information that has been added using a form and indexed in the catalog.

Introduction

I was asked to create a portlet that will display the birthdays of people of the current month. Initially they wanted to do something with a collection and maybe a manual page and change out the name of the collection and the name of the portlet every month. But I thought Plone could do something better, something more automated. Here is what I wanted to do;

  1. Create a form that my user could input the names of the users and the months of their birth. No other data was necessary. 
  2. Add a D2C object to the form. This will make each submission its own object so when a user leaves or wants to be removed its easy to remove them. 
  3. Add an index to the catalog so I can access the information quickly.
  4. Make a page template that would populate a portlet. That portlet needs to
    1. Have the current month in the title bar
    2. Display the users that have the birthdays matching the current month

Create the form

This is really the easiest part. 

  • Create your form with 
    • 1 string field (name) 
    • one selection field (month). The selection field should have 12 options one for each month. note I did not use a date field here because I didnt want people to opt out because your asking for a year (people get weird about displaying their age). 
    • Also add a "Save Data to Content Adapter". Remember what you named this.

Create the new index in the Catalog

We now need to create 2 new indexes in the catalog one for name and one for month
  • go to your catalog in the ZMI "plonesite/manage/portal_catalog/
  • choose the "indexes" tab
  • Add a new "KeywordIndex" and id it "GetBirthDate" 
    This will hold our Month information but remember this is really only text its not really a date as plone things of those things
  • Choose "add"
  • Add a new "KeywordIndex and id it "GetBirthName"
    This is the name of the user. Again this is just text

Populate the INDEXs

  • Go to the "custom folder" of your plone site "plonesite/manage/portal_skins/custom"
  • Create a new Script (python) 
  • Give that script the ID of "GetBirthDate"
  • Choose "add and edit" 
  • Copy and paste the following
if hasattr(context, 'portal_type') and context.portal_type == 'FormSaveData2ContentEntry' and hasattr(context, 'birth-month'):
  return context.getValue('birth-month')
else:
  return None
  • Choose "Save"

Now create the second python script for the birthname

  • Create a new Script (python) 
  • Give that script the ID of "GetBirthDate"
  • Choose "add and edit" 
  • Copy and paste the following
if hasattr(context, 'portal_type') and context.portal_type == 'FormSaveData2ContentEntry' and hasattr(context, 'name'):
  return context.getValue('name')
else:
  return None
  •  Choose "Save"

Create the "Page Template" 

  • Go to the "custom folder" of your plone site "plonesite/manage/portal_skins/custom"
  • Create a new "Page Template"
  • Name the page template "birthdayPortlet"
  • Copy and paste the following;
<html>
<body>
<div id="birthdayPortlet" metal:define-macro="portlet">
<dl>
<dt class="portletHeader">Birthdays in <span tal:replace="python:DateTime().Month()" /></dt>
</dl>
<dl tal:define="DateTime python:modules['DateTime'].DateTime; thisMonth python: DateTime().Month();
results python:context.portal_catalog.searchResults( GetBirthDate= thisMonth);">
<dd tal:repeat="result results"
        tal:content="result/Title"
>
          Birthdays
        </dd>
     </dl>
</div>
</body>
</html>
  • Save

Create a Classic Portlet to display the Page Template 

  • Open a page you want the template to display on
  • Choose "manage portlets"
  • Add a new "Classic portlet"
  • Give it the name of the template you want to display "birthdayPortlet"
  • Leave "portlet" as the macro
  • Choose "save" 

Notes

While the code above works for my uses, please be aware im a monkey when it comes to code. I hammer and hammer untill I get something close to what I want. Or I beg Nathan or David to help me :). I am quite sure that you (the reader) will have plenty of better more efficient options. 

I also went back in later and added a "quick edit" button on the portlet so that if they have permissions they can choose the "quick edit" and they would be taking directly to the add birthday form, rather than having to find or search for the form. I didn't add it to the code above.

 
 
 
 
 
 
 
 
 
 
Navigation