Help - set widget for parent 'description' field of Dexterity?

hi,

in Plone 5 I have class Actuality defined as:

@implementer(IActuality)
class Actuality(ownBaseItem, NewsItem):
"""
ownBaseItem BaseItem is subclassed from Container
"""

I need to set some whatUseIsWhatYouGet widget to 'description' of Actuality,
in Plone 4 it is so simple:

ActualitySchema['description'].widget = atapi.RichWidget(
label = _(u'Description'),
)

but in Plone 5 i dont know how to access to parent 'description' field.
Please give a hint.

Many thanks for you time.

Hello Vincent,

With Dexterity there are multiple levels where you can hook into, and they are a bit more strict than the Archetypes system and iirc use the component architecture more. But I must admit most of my Archetypes knowledge has faded.

The class from your code above is the class that implements the actual content item object. If you want to change the widget for a field defined in your IActuality Schema (which is an Interface from zope.interface) you can/should do so with directives on the IActuality schema itself. See 36. Dexterity: Reference – Mastering Plone 5 Development — Plone Training 2022 documentation chapter 36.5. With directives you can set the widget used and pass extra parameters.

The extra complication here is that the Description field is not on the 'main' schema of the content item, but is activated through a behavior. (Think SchemaExtenders v2). An axample to customise a 'behavior' field is discussed here: How can i set permissions for Widget/Field of a behavior with plone.autoform?.

Behaviors are a 2 edged sword: they are very easy to add to a CT if you 100% need their functionality and styling by adding one line to the GS profile field that configures the activated behaviors on a content type and thus its items, but if you want to customise fields it's sometimes easier to define the field in your CT's main schema. (like a 'text' field or add the Text behavior). Plone's default content types are 99% based on compositions of behaviors to keep things modular and defined only once. (iirc only the Link CT has a field in its main schema :open_mouth: )

If you want to do more customisation than is available with the directives you can add methods to the implementation class that modify/extend the IActuality schemata after they have been loaded for the Item.

The widgets however are only used on 'forms' where you can add or edit the content item. These form objects default to implementations that run over the fields defined in the schema for an item, but you can override both Add and/or Edit forms (see for example this thread [Solved] Behavior IPublication.expires - How to change the default value of "required" Attribute - #6 by 1letter or Plone 6 classic custom add/edit form)

The forms, directives and ZCA hook in points are mostly defined by z3c.form, the forms framework used (and different from Archetypes).

1 Like

Thank you @fredvd for long explanation :slight_smile:

i dont want to use edit/add forms as solution :slight_smile:

it's crazy, we have 'plone.basic' behavior at
..../eggs/plone.app.dexterity-2.6.10-py3.8.egg/plone/app/dexterity/behaviors/metadata.py

@provider(IFormFieldProvider)

> class IBasic(model.Schema):
> 
>     # default fieldset
> 
>     title = schema.TextLine(
> 
>         title=_(u'label_title', default=u'Title'),
> 
>         required=True
> 
>     )
> 
>     description = schema.Text(
> 
>         title=_(u'label_description', default=u'Summary'),
> 
>         description=_(
> 
>             u'help_description',
> 
>             default=u'Used in item listings and search results.'
> 
>         ),
> 
>         required=False,
> 
>         missing_value=u'',
> 
>     )

i need somehow to subclass my class IActuality(model.Schema, )
from IBasic but it says 'plone.app.dexterity.behaviors.metadata.IBasic' is not a package.

we must have short way to change widget for description from IBasic :slight_smile:

I came across this topic when dealing with the dublincore behavior, as this uses the basic behavior by inheritance.

it is a minefield to subclass/inherit/mix behaviours (like we did with atct) and to use this with composition (fti and named behaviors) for a certain type.

maybe reimplement/overriding IBasic by registering the behavior against your app-layer and do the tricks you want IBasic to perform, will do.
without any warranty from my side, because: IBasic is so low-level and deep down the rabbit hole ("basic" kinda gives a hint to that) - I would only walk there with a very bright torch and very very cautious.

Thx @fredvd @iham
I have problem with custorm Add Form for my Actuality content type

in browser/configure.zcml i have:

<adapter
    for="Products.CMFCore.interfaces.IFolderish
        zope.publisher.interfaces.browser.IDefaultBrowserLayer            
        plone.dexterity.interfaces.IDexterityFTI"
    provides="zope.publisher.interfaces.browser.IBrowserPage"
    factory=".fooActuality.AddView"
    name="Actuality"
    />
<class class=".fooActuality.AddView">
    <require
        permission="cmf.AddPortalContent"
        interface="zope.publisher.interfaces.browser.IBrowserPage"
        />
</class> 

in browser/fooActuality.py I have:

class AddForm(add.DefaultAddForm):
    portal_type = 'Actuality'

    def updateFields(self):
        super(AddForm, self).updateFields()        
        try:
            self.fields['IBasic.description'].widgetFactory = WysiwygFieldWidget
        except:
            pass

class AddView(add.DefaultAddView):
    form = AddForm

but when adding a new Actuality i dont see tinyMCE editor for description field.
Does anyone see where is the problem?

Many thanks.

The Description field is not normally rich text. If the field is rich text, you might want to use RichTextWidget (?)

hi @espenmn,
the description is from IBasic:
description = schema.Text(.....)

i better keep plone.basic behavior :slight_smile:

the same code
self.fields['IBasic.description'].widgetFactory = WysiwygFieldWidget
worked fine for Edit form :slight_smile:

PSL somehow i can not get into debug of AddForm init :frowning:

So you want to 'use html' for the Description ?

Not sure, but maybe that could lead to metadata/indexing problems. Try searching this 'forum', I think it has been discussed before.