Search code examples
plonedexterity

Accessor and Mutator Methods in Dexterity


I'm beginner user of dexterity (about 2 days now). I'm trying to migrate my old content type to dextertiy content in process of migrating website.

Schema defination in classic archetype is like

TextField('script',
          searchable=0,
          mutator="write",
          accessor="__call__",
          edit_accessor="document_src",
          widget=TextAreaWidget(label="Page Template script",rows=40,cols=40),

How can I redefine in dexterity ? I'm upgrading from Plone 252 to Plone 412.

Regards,


Solution

  • You will have to create a new Dexterity content type from scratch and completely rewrite your Archetype's Schema to a new schema that inherits from plone.directives.form and with the field types form zope.schema.

    For more information, see here: http://plone.org/products/dexterity/documentation/manual/developer-manual/schema-driven-types/referencemanual-all-pages

    For example, your Archetype's schema field declaration, will look like something like this in Dexterity:

    script = schema.TextLine(
            title=_(u"Page Template Script"),
        )
    

    Dexterity content types don't get automatic accessors and mutators like Archetypes content types. Instead, you just access the schema field as if it's an attribute.

    For example:

    script = myfolder.script
    

    If you want to create the same accessors and mutator (like you specified in the Archetypes field), you'll have to create them manually on your Dexterity class.

    For example, something like:

    class MyFolder(dexterity.Container):
        """ """
        grok.implements(IMyFolderSchema)
    
        def __call__(self):
            return self.script
    
        def edit_accessor(self):
            return self.script
    
        def write(self, value):
            self.script = value