plonegenericsetup

Add new record to plone.registry without re-running GenericSetup / re-installing the product


in a Plone add-on product, I have a control panel page where some config options can be set. They are stored in plone.registry. The control panel adapter fetches the different fields in its __init__ method by querying the interface, like:

class MultiLanguageExtraOptionsAdapter(LanguageControlPanelAdapter):
    implementsOnly(IMultiLanguageExtraOptionsSchema)

    def __init__(self, context):
        super(MultiLanguageExtraOptionsAdapter, self).__init__(context)
        self.registry = getUtility(IRegistry)
        self.settings = self.registry.forInterface(
            IMultiLanguageExtraOptionsSchema)

Now I add an additional field to the interface IMultiLanguageExtraOptionsSchema and restart plone. On the control panel page I then an error:

KeyError: 'Interface `plone.app.multilingual.interfaces.IMultiLanguageExtraOptionsSchema` defines a field `blah`, for which there is no record.'

(This is expected for the forInterfacemethod , as described on the plone.registry README. The record is not there.)

Of course, if I add that field via GenericSetup (registry.xml), and I re-install the product / re-run the "Control Panel" step, all is well:

<registry>
 <records interface="plone.app.multilingual.interfaces.IMultiLanguageExtraOptionsSchema">
   <value key="blah"></value>
 <records>
<registry>

But I don't want to force users to re-install a product, just because there's a new option in the product-specific control panel. So my question: Is there a recommended way for getting a new record for a new field into plone.registry?


Solution

  • If you pass False as the second parameter to forInterface:

    registry.forInterface(IMultiLanguageExtraOptionsSchema, False)
    

    then it won't throw an error if fields from the schema are missing from the registry, but will simply return the field's default value.