Home Articles New submit button for Archetypes edit template
Document Actions

New submit button for Archetypes edit template

Simple example how to extend Archetype template to add additional button and custom action for it.

Simple example how to extend Archetype template to add additional button and custom action for it.

About

This article shows how to solve simple use case. Lets say that we have few content types and we want to add an additional button and action that will be executed after it is pressed. Example presented below is about "edit and publish" button which saves changes and automatically publishes object.

Archetypes magic

Simple and ugly solution is to change base_edit template for each of those types. But this solution has so many obvious disadvantages that it is not even worth to mention it. We are smart and we use what Archetypes offer. We will use capability that when base_edit is rendered it searches for template <content_type>_edit. (For content type named Page it will be page_edit.) If it is found basic macros are retrieved from it instead of using default ones from edit_macros. We can redefine all of macros:

  • header
  • typedescription
  • body
  • footer
We can also redefine slots, that are inside macros, like we'll do with extra_buttons.

New template

So lets create Page Template page_edit.pt. Inside we will define new macro body which will override standard one. But in reality we will use standard body macro from edit_macros.pt. We will only fill slot extra_buttons.

<metal:macro define-macro="body"> <metal:macro use-macro="here/edit_macros/macros/body"> <metal:slot fill-slot="extra_buttons"> <metal:macro use-macro="here/content_macros/macros/extra_buttons"/> </metal:slot> </metal:macro> </metal:macro>

New button

As you probably noticed we are using macro extra_buttons from new template content_macros.pt. It is very simple, defines just one button. The most important thing is parameter name which is responsible for action called after submission. It this case script must be called edit_and_publish.py

<div metal:define-macro="extra_buttons"> <input class="context" type="submit" name="form.button.edit_and_publish" value="Save and Submit" i18n:attributes="value label_edit_and_publish;" tal:attributes="tabindex tabindex/next; disabled python:test(isLocked, 'disabled', None);" tal:condition="here/is_edition_enabled"/> </div>

Metadata file

In order to make our future button to work we must add its action to base_edit.cpt.metadata file. We have to copy it to our skin from Plone skin and add one line at the beginning in actions section.

[default] ... [validators] ... [actions] action.success..edit_and_publish = traverse_to:string:edit_and_publish ...

Python script for action

This Python Script edit_and_publsih.py should be added to skins. It process all fields from form to save data and then change workflow state. After success it redirects to object view with status message. This is of course an example and we can add anything we want here.

## Script (Python) "edit_and_publish" ##title=Edit content ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind state=state ##bind subpath=traverse_subpath ##parameters= ## from Products.CMFCore.utils import getToolByName #Save changes normal way context.processForm() portal_workflow = getToolByName(context, 'portal_workflow', None) #change workflow if portal_workflow.getInfoFor(context,'review_state') != 'published': try: portal_workflow.doActionFor(context,'publish') portal_status_message = "Changes saved and document publsihed" except: pass return context.REQUEST.RESPONSE.redirect("%s?portal_status_message=%s"%( context.absolute_url(),portal_status_message))

Sponsor me

I'll run London Marathon on 26th of April 2009. Please help me with fundraising for Sense - charity for deafblind people. Justgiving - SPONSOR ME

Tags