简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM