Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to extend Plone Dexterity's INameFromTitle behavior?

The project I am working on uses Plone's awesome Dexterity plugin. A couple of my custom content types have very specific names that must be computed. The way I had originally accomplished this before was by adding plone.app.content.interfaces.INameFromTitle as a behavior in the object's generic setup entry, per the manual's directions:

<?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>

Then I created an adapter that would provide 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

This method is very similar to that suggested in this blog post:

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

Unfortunately, this method stopped working after plone.app.dexterity beta and now my content items don't have their names assigned properly.

Would anyone happen to know how to extend Dexterity's INameFromTitle behavior for very specific naming use-cases?

Your help is much appreciated, thanks!

like image 385
mmartinez Avatar asked Nov 01 '11 23:11

mmartinez


1 Answers

You could try the following.

in interfaces.py

from plone.app.content.interfaces import INameFromTitle

class INameForMyType(INameFromTitle):

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

in behaviors.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

I generally prefer defining my adapters using ZCML; in configure.zcml

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

but you could probably also use a grok.global_adapter.

like image 59
Rigel Di Scala Avatar answered Sep 30 '22 17:09

Rigel Di Scala