September 26, 2006

Obtaining the backward references

It seems to be a common requirement to do not only obtain the list of related items (references to other objects) but also to obtain the list of back references.
The following snippet added to document_related.pt will show all back references:

 <tal:referencedBy tal:define="backRefs context/getBRefs">
            <fieldset id="backReferences" tal:condition="backRefs">
                <legend i18n:translate="label_back_references">Back references</legend>
                <ul class="visualNoMarker">
                    <tal:backRef repeat="item backRefs"
                                         define="use_view_action site_properties/typesUseViewActionInListings|python:();">
                        <li tal:define="
                                desc                item/Description;
                                item_type item/portal_type;
                                item_type_class python: 'visualIcon contenttype-' + normalizeString(item_type);
                                item_wf_state       item/review_state|python: getInfoFor(item, 'review_state', '');
                                item_wf_state_class python: 'state-' + normalizeString(item_wf_state);
                                item_url           item/absolute_url;
                                item_url           python:test((item_type in use_view_action), item_url+'/view', item_url)">

                            <span tal:attributes="class item_type_class">
                            <a href="" class=""
                                tal:attributes="href  item_url;
                                                title item/Description;
                                                class string: $item_wf_state_class visualIconPadding"
                                tal:content="item/pretty_title_or_id">
                                Related Item
                            </a>
                            </span>
                        </li>
                    </tal:backRef>
                 </ul>
            </fieldset>
        </tal:referencedBy>