How do I get a field value from parent containers if the current content's same field is empty? Plone 4

StackOverflow https://stackoverflow.com/questions/18365866

  •  26-06-2022
  •  | 
  •  

Question

I have added an attribute to all Folders using schema extension. The new attribute is called greetingText. Each folder can have greetingText throughout the chain A > B > C, however I have defaulted greetingText to empty string (''). If B has a greetingText and the current context is for object B, then it should show B greetingText. If folder B or C has empty string for its greetingText, then if object B is the current context, I want obj.getFields('greetingText') to give the container folder A's greetingText instead. I understand that this is how Acquisition works, yet I think it only works this way if folder B's greetingText was None rather than an actual value, which is empty string. This is from the context of a Controller Python Script connected to a Controller Page Template.

from Products.CMFPlone import PloneMessageFactory as _
from Products.CMFCore.utils import getToolByName

plone_utils = getToolByName(context, 'plone_utils')
plone_log=context.plone_log

req = context.REQUEST
res = req.RESPONSE

greeting = context.getField('greetingText').getAccessor(context)()

msg = "id= %s"%(greeting)

res.write(msg)

return
Was it helpful?

Solution

The aq_parent attribute of an object allows you to get at the parent. You can climb the object hierarchy looking for a greeting:

greeting = ''
while context is not None:
    accessor = getattr(context, 'getGreetingText')
    if accessor:
        greeting = accessor()
        if greeting:
            break
    context = context.aq_parent

This assumes you haven't changed the name of the Archetypes accessor for the field.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top