How to get fields displaying in a custom plone dexterity form?

453 views Asked by At

I'm trying to avoid writing out a few hundred fields in my custom addform for a plone dexterity object.

i've created a loop that is called from my customVisitFormTemplate.pt

    def otherFields2(self):
    #print "this gets called3"
    customs=""

    fields = field.Fields(ISiteVisit)

    #print dir(fields)
    for r in fields:
        #print dir(r)
        #print r.title()
        if r.startswith("current") or r.startswith("landCover") or r.startswith("surrounding"):
            pass

        else:
            print 'in others', r
            customs=customs+"""<tal:field tal:replace='structure view/widgets/%s/@@ploneform-render-widget'/>""" % (r)
    print customs
    return customs

in the custom template i call it with this:

    <fieldset>
      <legend>General Info</legend>
      <span tal:define="otherFields view/otherFields2">
        <div tal:content="structure otherFields" />
       </span>
    </fieldset>

however, on execution the tal statement does not call the widget, and it outputs to html:

<tal:field tal:replace="view/widgets/siteID/@@ploneform-render-widget" />

if i use the following code directly in my custom temlpate:

<tal:field tal:replace="view/widgets/siteID/@@ploneform-render-widget" />

it outputs to html and it works:

<div id="formfield-form-widgets-siteVisitNotes" class="field z3cformInlineValidation kssattr-fieldname-form.widgets.siteVisitNotes">
<label class="horizontal" for="form-widgets-siteVisitNotes"> Site Visit Notes </label>
<div class="fieldErrorBox"></div>
<textarea id="form-widgets-siteVisitNotes" class="textarea-widget text-field" name="form.widgets.siteVisitNotes"></textarea>
</div>

how do i get my looped code from my .py file to output the same as the "direct" code?

thanks for any suggestions

1

There are 1 answers

0
Don Keillor On

after quite some playing around, i finally got this complexity working :).

      <fieldset>
            <legend>General Info</legend>
            <div>
                <metal:define define-macro="widget_rendering">    <!-- not sure if this define-macro is needed -->
                    <span tal:define="widgets view/widgets/values">
                        <tal:widgets repeat="widget python:[w for w in widgets if not w.name.startswith('form.widgets.current') and not w.name.startswith('form.widgets.surrounding') and not w.name.startswith('form.widgets.landCover')]">

                            <metal:field-slot define-slot="field">
                                <metal:field define-macro="field">
                                    <tal:widget tal:condition="python:widget.id !='form-widgets-url'"
                                        tal:replace="structure widget/@@ploneform-render-widget"/>
                                </metal:field>
                            </metal:field-slot>
                            <!--</span>-->
                        </tal:widgets>
                    </span>
                </metal:define>
                <br>    <!-- to keep label and input field together -->
            </div>
        </fieldset>

here are still an issue in terms of having the title and input box stay together when resizing the page, but the sorting code works.