首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我的自定义collective.opengraph适配器不被Plone使用

我的自定义collective.opengraph适配器不被Plone使用
EN

Stack Overflow用户
提问于 2014-11-26 20:08:10
回答 1查看 102关注 0票数 2

我在告诉Plone为集体.开图 viewlets.ATMetatags使用自定义适配器时遇到了问题。

我遵循了包的自述文件中的说明,最后得到了以下内容:

在这里,我的主题的包文件夹结构:

代码语言:javascript
运行
复制
.
├── adapters.py 
├── behaviors.py
├── behaviors.zcml
├── completer.hist
├── configure.zcml
├── __init__.py
├── interfaces.py
├── portlets
├── profiles
├── resources
├── testing.py
└── tests

这是我的adapters.py文件:

代码语言:javascript
运行
复制
# -*- coding: utf-8 -*-

from zope.interface import implements
from zope.component import getMultiAdapter, adapts

from collective.opengraph.interfaces import IOpengraphMetatags
from collective.opengraph.viewlets import ATMetatags, decode_str


class MyOpengraphMetatags(ATMetatags):

    implements(IOpengraphMetatags)
    adapts(ATMetatags)

    def __init__(self, context):
        print "MyOpengraphMetatags.__init__!"
        super(MyOpengraphMetatags, self).__init__(self, context)


    @property
    def image_url(self):
        """
            don't use the default plone logo !
        """
        return "test.png"

    @property
    def description(self):

        context = self.context.aq_inner
        portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state')
        current_language = portal_state.language

        description_fr = "fr"
        description_en = "en"
        description = description_fr if current_language == "fr" else description_en

        return decode_str(description, self.default_charset)

    @property
    def metatags(self):
        """
            Adding custom tags !
        """
        tags = super(MyOpengraphMetatags, self).metatags

        tags.update({
            "twitter:site": "@a-twitter-account"
        })

        return tags

最后,我的configure.zcml文件:

代码语言:javascript
运行
复制
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    xmlns:genericsetup="http://namespaces.zope.org/genericsetup"
    xmlns:plone="http://namespaces.plone.org/plone"
    i18n_domain="my.theme">

  <includeDependencies package="." />

  <!-- Include files -->
  <include file="behaviors.zcml" />

  <include package=".portlets" />

  <plone:static
    directory="resources"
    type="theme"
    />

  <genericsetup:registerProfile
      name="default"
      title="my.theme"
      directory="profiles/default"
      description="Installs the my.theme package"
      provides="Products.GenericSetup.interfaces.EXTENSION"
      />

  <!-- -*- extra stuff goes here -*- -->
  <adapter 
    for="collective.opengraph.viewlets.ATMetatags"
    factory=".adapters.MyOpengraphMetatags"
    provides="collective.opengraph.interfaces.IOpengraphMetatags"
  />

</configure>

我的适配器由Plone检测到,但根本没有使用。

我还在其中添加了pdb.set_traceprint指令,但没有成功。

但是,在collective.opengraph鸡蛋中添加这些指令是可行的。

我是不是忘了什么配置?适配器标记是否位于适当的configure.zcml文件中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-26 20:37:28

如果您想直接覆盖适配器,可以通过覆盖已经注册的适配器来做到这一点。您的overrides.zcml可能如下所示:

代码语言:javascript
运行
复制
<configure
    xmlns="http://namespaces.zope.org/zope"
    xmlns:i18n="http://namespaces.zope.org/i18n"
    i18n_domain="my.theme">

    <adapter 
        for="Products.ATContentTypes.interface.interfaces.IATContentType"
        factory=".adapters.MyOpengraphMetatags"
        provides="collective.opengraph.interfaces.IOpengraphMetatags"
        />

</configure>

顺便说一句。适配器注册中的for属性需要是从Interface (zope.interface.Interface)继承的类的虚名。collective.opengraph中的适配器是为Products.ATContentTypes.interface.interfaces.IATContentType注册的。这意味着适配器仅适用于实现Products.ATContentTypes.interface.interfaces.IATContentType接口的对象(通常是基于内容类型的原型)。

还可以从implements(IOpengraphMetatags)adapts(ATMetatags)中删除MyOpengraphMetatags,因为您已经通过适配器注册完成了这一操作。adapts(ATMetatags)没有任何意义。

如果您有自己的不基于原型的内容类型,它实现了自己的接口。您可以在configure.zcml中注册适配器。

代码语言:javascript
运行
复制
    <adapter 
        for="dotted.name.to.your.interface"
        factory=".adapters.MyOpengraphMetatags"
        provides="collective.opengraph.interfaces.IOpengraphMetatags"
        />

下面是OP步骤中的完整解决方案:

  1. 将适配器标记移动到overrides.zcml文件,并从configure.zcml文件中删除它
  2. 确保要重写的包是在构建包中添加的。
  3. 运行构建
  4. 在fg模式下启动Plone。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27158007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档