Create content error on IObjectAdded event in Plone 6.10 and 6.11

I have been programmatically creating content via events. However, in the current Plone versions 6.0.10.1 and 6.0.11.1 my usual strategy no longer works. Below is my example code:

def init_user_account(context,event):
    types_tool = api.portal.get_tool(name='portal_types')
    ctype_factory = types_tool.getTypeInfo("Account")
    ctype_factory._constructInstance(context,'test-id',title='Test Title')
    #api.content.create(type='Property Account',id='test-id',title='Test Title',container=context)

The error that I get using _constructInstance is the following:

  ...
  Module Products.CMFCore.WorkflowTool, line 297, in notifyCreated
  Module Products.DCWorkflow.DCWorkflow, line 334, in notifyCreated
  Module Products.DCWorkflow.DCWorkflow, line 418, in _changeStateOf
  Module Products.DCWorkflow.DCWorkflow, line 511, in _executeTransition
  Module Products.DCWorkflow.Expression, line 129, in createExprContext
AttributeError: getPhysicalRoot

If I use the content API, the code simply does not work. I am guessing the same error occurred.

Below is where the error occurs in the source code (~/.buildout/eggs/Products.DCWorkflow-3.0-py3.11.egg/Products/DCWorkflow/Expression.py):

def createExprContext(sci):
    '''
    An expression context provides names for TALES expressions.
    '''
    ob = sci.object
    wf = sci.workflow
    container = aq_parent(aq_inner(ob))
    data = {
        'here':         ob,
        'object':       ob,
        'container':    container,
        'folder':       container,
        'nothing':      None,
        'root':         ob.getPhysicalRoot(),
        'request':      getattr(ob, 'REQUEST', None),
        'modules':      SecureModuleImporter,
        'user':         getSecurityManager().getUser(),
        'state_change': sci,
        'transition':   sci.transition,
        'status':       sci.status,
        'kwargs':       sci.kwargs,
        'workflow':     wf,
        'scripts':      wf.scripts,
        }
    return getEngine().getContext(data)

I recall reading in the forum( or was it on Github?) about a security feature being "reverted back to strict mode". However, I could not recall the exact issue and the package where the change occurred. My problem could be related since the part where the error occurred is about initializing variables available to workflow expressions. Also, if I use dir() on the object, the method getPhysicalRoot is listed but an attribute error is returned if it is called.

Does any one remember the recent change I am referring to or the package involved?

h2o via Plone Community wrote at 2024-6-30 09:10 +0000:

...
I have been programmatically creating content via events. However, in the current Plone versions 6.0.10.1 and 6.0.11.1 my usual strategy no longer works. Below is my example code:

def init_user_account(context,event):
   types_tool = api.portal.get_tool(name='portal_types')
   ctype_factory = types_tool.getTypeInfo("Account")
   ctype_factory._constructInstance(context,'test-id',title='Test Title')
   #api.content.create(type='Property Account',id='test-id',title='Test Title',container=context)

The error that I get using _constructInstance is the following:

 ...
 Module Products.CMFCore.WorkflowTool, line 297, in notifyCreated
 Module Products.DCWorkflow.DCWorkflow, line 334, in notifyCreated
 Module Products.DCWorkflow.DCWorkflow, line 418, in _changeStateOf
 Module Products.DCWorkflow.DCWorkflow, line 511, in _executeTransition
 Module Products.DCWorkflow.Expression, line 129, in createExprContext
AttributeError: getPhysicalRoot

Maybe, you use a handler for IObjectCreatedEvent?
At this time, the object is not yet inserted into the web site
hierarchy and getPhysicalRoot may fail (it walks upward until it
finds the root).

Likely, a handler for IObjectAddedEvent is better. At this
time, getPhysicalRoot should succeed on the added object.

You are indeed correct. :man_facepalming:

While I meant to use IObjectAddedEvent, I typed (or maybe copy-pasted) the wrong one. I never bothered to double check my configuration and I thought I was encountering a bug.

Thank you for suggesting to check it!