简体   繁体   English

Plone - 覆盖Zope Schema字段

[英]Plone - Override Zope Schema fields

In using Plone, I have integrated DocumentViewer product into my Plone application to help in viewing PDF files. 在使用Plone时,我已将DocumentViewer产品集成到我的Plone应用程序中,以帮助查看PDF文件。 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. 现在我想通过在example.product中覆盖它来向IGlobalDocumentViewerSettings接口添加另一个字段。

I don't think I can use SchemaExtender since they are not Archetypes. 我不认为我可以使用SchemaExtender,因为它们不是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文件browserlayer.xml添加到您的配置文件中(确保重新安装产品以便层注册)::

<?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. 哇,这太难了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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