How to show the image of a Dexterity-based Plone content type in a listing view?
Asked Answered
E

4

5

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?

Endbrain answered 20/9, 2011 at 13:52 Comment(1)
I had a similar question and i got those answers. usarios-plone.2295514.n2.nabble.com/…Aludel
L
5

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.

Lingam answered 20/9, 2011 at 17:33 Comment(1)
Right, I forgot that the image data for blobs is stored separately anyway.Endbrain
Q
4

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. :)

Quasimodo answered 20/9, 2011 at 15:0 Comment(3)
Thanks for the hint! I will have a look and paste the answer here.Endbrain
Please, be sure to not post any copyrighted material here. Provide your own answer to the question.Clearly
zedr - I wrote that book, and I won't post materials from it directly. I just wanted to provide Timo with an answer even if I didn't have time to look it up.Quasimodo
I
0

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.

Imparity answered 21/9, 2011 at 7:52 Comment(0)
N
0

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
Nadeen answered 2/9, 2012 at 0:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.