I would like to add some javascript to both my Add and Edit forms only if a behavior marker is present.
Good docs here https://docs.plone.org/external/plone.app.dexterity/docs/advanced/custom-add-and-edit-forms.html and here https://training.plone.org/5/javascript/exercises/2.html
Also, this javascript should be included if a behavior is enabled. For this example, the javascript file can contain alert("script is present");
and is registered as a javascript resource. See docs above in how to register a js resource.
The edit form was easy - the trick is to set the browser view for
to the behavior instead of the type:
<browser:page
name="edit"
for=".behaviors.IJavascriptBehavior"
class=".browser.EditView"
permission="cmf.ModifyPortalContent"
/>
file .browser:
from Products.CMFPlone.resources import add_resource_on_request
from plone.dexterity.browser import edit
class EditView(edit.DefaultEditForm):
def __call__(self):
# utility function to add resource to rendered page
add_resource_on_request(self.request, 'MyJavascriptResource')
return super(EditView, self).__call__()
The add form, however, I can't seem to make work with ONLY THE BEHAVIOR - I have to set it for all add forms (for the 'story' portal type). It registers to the FTI - but how to I isolate it to a behavior?
Python is simple enough:
class AddView(add.DefaultAddView):
def __call__(self):
# utility function to add resource to rendered page
add_resource_on_request(self.request, 'MyJavascriptResource')
return super(AddView, self).__call__()
But the ZCML is quite different:
<adapter
for="Products.CMFCore.interfaces.IFolderish
zope.publisher.interfaces.browser.IDefaultBrowserLayer
plone.dexterity.interfaces.IDexterityFTI"
provides="zope.publisher.interfaces.browser.IBrowserPage"
factory=".browser.AddView"
name="story"
/>
<class class=".browser.AddView">
<require
permission="cmf.AddPortalContent"
interface="zope.publisher.interfaces.browser.IBrowserPage"
/>
</class>
I thought replacing 'IFolderish' with my behavior would work, but even the docs say 'you must not do this' and it doesn't work.
Maybe a layer? But I don't want to get into the whole 'most specific layer' problem even if it does work.
Any thoughts or experience you can share?