How do you override the default value of a field in a dexterity behavior in Plone?
Asked Answered
C

2

5

We have a requirement for a dexterity content type to have exclude from navigation behaviour but for the exclude_from_nav field's default value to be True. In the behaviour plone.app.dexterity.behaviors.exclfromnav.IExcludeFromNavigation it defaults to False.

Obviously I could create my own behaviour that copies IExcludeFromNavigation except for the default value but I was wondering if there was a way to do this based on reusing IExcludeFromNavigation. We have other content types that use IExcludeFromNavigation where we do want it to default to False.

We're using Plone 4.1rc3 and Dexterity 1.0

Cum answered 12/7, 2011 at 10:4 Comment(0)
C
3

I have this working using a plone.directives.form decorator.

I've added this to one of my behaviour modules.

from plone.directives.form import default_value

@default_value(field = IExcludeFromNavigation['exclude_from_nav'])
def excludeFromNavDefaultValue(data):
    return data.request.URL.endswith('++add++my_item_type')

I also have the following in configure.zcml

<include package="plone.directives.form" file="meta.zcml" />
<include package="plone.directives.form" />

<grok:grok package="." />

Thanks to Martin for the large clue although his answer didn't quite solve my problem. This feels like a bit of a hack to me - a more elegant solution would be nice.

Cum answered 12/7, 2011 at 14:28 Comment(3)
Another option would be to register a custom add form and then use the 'form' (or is it 'view'?) discriminator to default_value() to specify an override for that form only.Hodman
Thanks, i used this for my scenario where i have two different object types in the same container i want to exclude by default. Problem was i couldnt put the same decorator on both object definitions or i would get config conflict complaints. I ended up just declaring on one of the types:: return data.request.URL.endswith('++add++my_item_type') or data.request.URL.endswith('++add++my_other_type')Lycanthrope
and like your example i had to remove the context parameter for it to work at all... seems hacky indeed.Lycanthrope
H
5

See http://plone.org/products/dexterity/documentation/manual/developer-manual/advanced/defaults and http://pypi.python.org/pypi/plone.directives.form#value-adapters, but basically:

@form.default_value(field=IExcludeFromNavigation['exclude_from_nav'], context=IMyType)
def excludeFromNavDefaultValue(data):
    return True

Cheers, Martin

Hodman answered 12/7, 2011 at 12:10 Comment(2)
Martin. Thanks for your answer but it isn't quite what I'm after. I may be wrong but context here seems to be the container to which you're adding the item and I need a rule based on the type of item being added. I can see an ugly hack using a test of data.request.URL but something more elegant would be niceCum
First link is broken. Use: 5.docs.plone.org/external/plone.app.dexterity/docs/advanced/…Stravinsky
C
3

I have this working using a plone.directives.form decorator.

I've added this to one of my behaviour modules.

from plone.directives.form import default_value

@default_value(field = IExcludeFromNavigation['exclude_from_nav'])
def excludeFromNavDefaultValue(data):
    return data.request.URL.endswith('++add++my_item_type')

I also have the following in configure.zcml

<include package="plone.directives.form" file="meta.zcml" />
<include package="plone.directives.form" />

<grok:grok package="." />

Thanks to Martin for the large clue although his answer didn't quite solve my problem. This feels like a bit of a hack to me - a more elegant solution would be nice.

Cum answered 12/7, 2011 at 14:28 Comment(3)
Another option would be to register a custom add form and then use the 'form' (or is it 'view'?) discriminator to default_value() to specify an override for that form only.Hodman
Thanks, i used this for my scenario where i have two different object types in the same container i want to exclude by default. Problem was i couldnt put the same decorator on both object definitions or i would get config conflict complaints. I ended up just declaring on one of the types:: return data.request.URL.endswith('++add++my_item_type') or data.request.URL.endswith('++add++my_other_type')Lycanthrope
and like your example i had to remove the context parameter for it to work at all... seems hacky indeed.Lycanthrope

© 2022 - 2024 — McMap. All rights reserved.