How to create objects with state of parent object?

On one of our websites the initial state, i. e. the state the object is created with, should be the same state as the parent object, i. e. the folder containing the new object. This document is a short description about how this was solved.

The solution we use creates objects in a default (failsafe) state. Afterwards, a script reads the parent state and sets the object’s state to the state of the parent. Here, the procedure is shown with the default workflow plone_workfow. Also, this needs to be done for the folder_workflow responible for the workflow managment of folderish objects.

Create a script

  • In the ZMI go to portal_workflow > plone_workflow and select the scripts tab
  • Add a new python script and give it the id setState
  • In the parameter list enter state_change
  • As code add the following:
       # set state to state of container
       # called by intialTransition upon creation of an object
    
       obj = state_change.object
    
       # get the parent state
       parent = obj.aq_parent
       try: 
         parentState = context.portal_workflow.getInfoFor(parent, 'review_state')
       except:
         # use default state of object if no review_state available for parent object
         parentState = context.portal_workflow.getInfoFor(obj, 'review_state')
    
       # get possible transitions for object in current state
       actions = context.portal_workflow.getActionsFor(obj)
       transitions = []
       for item in actions:
         transitions.append(item['transition'])
    
       # find transition that brings us to the state of parent object
       for item in transitions:
         if item.new_state_id == parentState:
           context.portal_workflow.doActionFor(obj, item.id)
    

Create a new transition

  • In the ZMI go to portal_workflow > plone_workflow and select the transitions tab.
  • Create a new transition called intialTransition
  • For this transition set the trigger type to automatic and as destination state select visible. As script (after) select the setState script defined above.

Add new initial state

  • In the ZMI go to portal_workflow > plone_workflow and select the states tab.
  • Create a new state called initialState and make it the default state.
  • As possible transitions select all transitions that give feasable states. Here, these were hide, initial transition, publish and show

Thats it 🙂