Question

What's the best way to show the image of a Dexterity-based Plone content type in a listing view?

Say I have a folder with Dexterity-based content objects that provide an image field and I want to list the objects (as catalog brains) together with their image. It is possible to show the images in the listing by calling their absolute URL:

<img src="" tal:attributes="src string:${item/getURL}/@@images/image/thumb" />

Though, Plone will raise an error if the image does not exist and I don't see a good way to check if the image exists in a page template. We obviously do not want to wake up the objects for a listing, to look up the image.

Do I have to create an image metadata column in the catalog or is there a better solution I don't see?

Was it helpful?

Solution

I wouldn't worry too much about waking up objects in a listing as long as it's properly batched. If you're using the fields from plone.app.textfield and plone.namedfile then large data is kept in separate persistent objects, so the main content item object is relatively lightweight. Of course, do your own benchmarking if you want to be sure it doesn't hurt for your case.

OTHER TIPS

There is a recipe for how to do this in Professional Plone 4 Development (chapter 10, I believe). Unfortunately, I genuinely can't remember how to do it right now. :)

Create a metadata in portal_catalog where you will store a path to a valid image. Create a python script of the same name that will check if the object has an image and in the case there is no image, return a default one.

A example for checking image field is avalable:

class product(grok.View):
    grok.context(IDexterityContent)
    grok.require('zope2.View')
    grok.name('view')

    def update(self):
        """
        """
        # Hide the editable-object border
        context = self.context
        request = self.request
        request.set('disable_border', True)


    #brain come from contained image field 's object
    def isImageAvalable(self,brain=None):
        """判断图片字段是否有效"""
        try:
            if brain is None:
                image = self.context.image.size
            else:
                 image = brain.getObject().image.size

            return (image != 0)

        except:
            return False
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top