7

我正在做的项目使用了 Plone 很棒的 Dexterity 插件。我的一些自定义内容类型具有必须计算的非常具体的名称。我之前最初完成此操作的方法是按照手册的说明在对象的通用设置条目中添加plone.app.content.interfaces.INameFromTitle作为行为:

<?xml version="1.0"?>
<object name="avrc.aeh.cycle" meta_type="Dexterity FTI">
  ...
  <property name="schema">myproject.mytype.IMyType</property> 
  <property name="klass">plone.dexterity.content.Item</property>
  ...
  <property name="behaviors">
    <element value="plone.app.content.interfaces.INameFromTitle" />
  </property>
  ...
</object>

然后我创建了一个可以提供 INameFromTitle 的适配器:

from five import grok
from zope.interface import Interface
import zope.schema
from plone.app.content.interfaces import INameFromTitle

class IMyType(Interface):

    foo = zope.schema.TextLine(
        title=u'Foo'
        )

class NameForMyType(grok.Adapter):
    grok.context(IMyType)
    grok.provides(INameFromTitle)

    @property
    def title(self):
        return u'Custom Title %s' % self.context.foo

此方法与此博客文章中建议的方法非常相似:

http://davidjb.com/blog/2010/04/plone-and-dexterity-working-with-computed-fields

不幸的是,这种方法在 plone.app.dexterity beta 之后停止工作,现在我的内容项没有正确分配它们的名称。

有人会碰巧知道如何为非常具体的命名用例扩展 Dexterity 的 INameFromTitle 行为吗?

非常感谢您的帮助,谢谢!

4

2 回答 2

4

您可以尝试以下方法。

接口.py

from plone.app.content.interfaces import INameFromTitle

class INameForMyType(INameFromTitle):

    def title():
        """Return a custom title"""

行为.py

from myproject.mytype.interfaces import INameForMyType

class NameForMyType(object):
    implements(INameForMyType)

    def __init__(self, context):
        self.context = context

    @property
    def title(self):
        return u"Custom Title %s" % self.context.foo

我通常更喜欢使用 ZCML 定义我的适配器;在configure.zcml

<adapter for="myproject.mytype.IMyType"
         factory=".behaviors.NameForMyType"
         provides=".behaviors.INameForMyType"
         />

但您也可以使用 grok.global_adapter。

于 2012-01-05T09:39:46.400 回答
3

我通过适应 INameFromTitle 的行为来做到这一点

行为.py

class INameFromBrandAndModel(Interface):
    """ Interface to adapt to INameFromTitle """

class NameFromBrandAndModel(object):
    """ Adapter to INameFromTitle """
    implements(INameFromTitle)
    adapts(INameFromBrandAndModel)

    def __init__(self, context):
        pass

    def __new__(cls, context):
        brand = context.brand
        model = context.modeltype    
        title = u'%s %s' % (brand,model)
        inst = super(NameFromBrandAndModel, cls).__new__(cls)

        inst.title = title
        context.setTitle(title)

        return inst

behavior.zcmlconfigure.zcml

<plone:behavior

    title="Name from brand and model"
    description="generates a name from brand and model attributes"
    for="plone.dexterity.interfaces.IDexterityContent"
    provides=".behavios.INameFromBrandAndModel"
    />

<adapter factory=".behaviors.NameFromBrandAndModel" />

然后禁用 INameFromTitle 中的行为profiles/types/your.contenttype.xml

瞧。这集成得很好,并在默认视图和导航中显示了正确的标题。从适配器中删除context.setTitle(title) 只会给我们留下一个正确的 id,但没有设置标题。

这不会在编辑后自动更改标题。到目前为止,klass正如经常建议的那样,我没有成功覆盖我的内容类型的属性。

如果您title在架构中定义属性,例如:

class IBike(form.Schema):
    """A bike
    """

    title = schema.TextLine(
        title = _(u'Title'),
        required = False,
    )

您可以稍后轻松更改标题。应该在 addForm 中隐藏标题字段,以避免误解。

于 2012-10-13T17:44:15.980 回答