简体   繁体   中英

Plone - Override Zope Schema fields

In using Plone, I have integrated DocumentViewer product into my Plone application to help in viewing PDF files. The document viewer add-on product has defined a set of schemas/fields which can be viewed in the control panel settings ie Site Setup -> Document Viewer Settings .

You can view how the fields/schemas have been defined here

Now I want to add another field to the IGlobalDocumentViewerSettings interface by overriding it in my example.product.

I don't think I can use SchemaExtender since they are not Archetypes. I've also tried following the instructions as provided by this link but to no avail. I am able to reinstall my product but the field I have added is not shown.

Here is a sample of my code:

from collective.documentviewer.interfaces import IGlobalDocumentViewerSettings
from collective.documentviewer.interfaces import IDocumentViewerSettings
from zope import schema
from zope.interface import implements

class DocViewerSchemaProvider(object):
    implements(IGlobalDocumentViewerSettings)

    def getSchema(self):
        """
        """
        return IEnhancedDocumentViewerSchema

class IEnhancedDocumentViewerSchema(IDocumentViewerSettings):
    """ 
    Use all the fields from the default schema, and add various extra fields.
    """

    folder_location = schema.TextLine(
        title=u"Default folder location",
        description=u'This folder will be created in the Plone root folder. '
                    u'Plone client must have write access to directory.',
        default=u"files_folder")

Can anyone help me on how to override this particular interface?

Since the author(me) didn't make it simple to override this, it'll be a little complicated to do your override. The following steps are what you'll need to do. Warning, it's all pseudo code though so you'll need to tweak perhaps to make it work for you.

First, provide your customized interface by extending the interface you want to customize:

class IEnhancedDocumentViewerSchema(IGlobalDocumentViewerSettings):
    """ 
    Use all the fields from the default schema, and add various extra fields.
    """

    folder_location = schema.TextLine(
        title=u"Default folder location",
        description=u'This folder will be created in the Plone root folder. '
                    u'Plone client must have write access to directory.',
        default=u"files_folder")

Then, create the settings adapter that is used to store and retrieve the settings of the schema::

from collective.documentviewer.settings import Base
class CustomSettings(Base):
    implements(IEnhancedDocumentViewerSchema)
    use_interface = IEnhancedDocumentViewerSchema

Then, register your adapter::

<adapter 
    for="Products.CMFPlone.interfaces.IPloneSiteRoot"
    provides="my.product.interfaces.IEnhancedDocumentViewerSchema"
    factory=".somewhere.CustomSettings" />

Then, create a form using your custom schema::

from z3c.form import field
from plone.app.z3cform.layout import wrap_form
from collective.documentviewer.views import GlobalSettingsForm
class CustomGlobalSettingsForm(GlobalSettingsForm):
    fields = field.Fields(IEnhancedDocumentViewerSchema)
CustomGlobalSettingsFormView = wrap_form(CustomGlobalSettingsForm)

Then, create a customization layer for your product, extending the documentviewer layer. This will require 2 steps. First, add the layer interface::

from collective.documentviewer.interfaces import ILayer as IDocumentViewerLayer
class ICustomLayer(IDocumentViewerLayer):
    """
    custom layer class
    """

And register your layer with generic setup. Add the the xml file, browserlayer.xml, to your profile with the following contents(make sure to reinstall the product so the layer gets registered)::

<?xml version="1.0"?>
<layers name="" meta_type="ComponentRegistry">
    <layer name="my.product" 
           interface="my.product.interfaces.ICustomLayer" />
</layers>

Finally, override the global settings view with your custom form just for the layer you have registered for your product::

<browser:page
    name="global-documentviewer-settings"
    for="Products.CMFPlone.interfaces.IPloneSiteRoot"
    class=".somewhere.CustomGlobalSettingsFormView"
    layer=".interfaces.ICustomLayer"
    permission="cmf.ManagePortal" />

Wow, that was way too difficult.

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