Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 426
scarba05 Avatar asked Jul 12 '11 10:07

scarba05


2 Answers

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

like image 88
optilude Avatar answered Oct 09 '22 03:10

optilude


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.

like image 37
scarba05 Avatar answered Oct 09 '22 05:10

scarba05