Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

volto.preview_image_link does not work OOTB #141

Closed
sneridagh opened this issue Apr 1, 2024 · 6 comments · Fixed by plone/plone.app.contentlisting#64
Closed

volto.preview_image_link does not work OOTB #141

sneridagh opened this issue Apr 1, 2024 · 6 comments · Fixed by plone/plone.app.contentlisting#64

Comments

@sneridagh
Copy link
Member

I'm trying to get the preview_image_link workable, but it seems that it is not working.

The scales never get into the serialization, I can't pinpoint why.

Steps to reproduce:

1.- Add the behavior volto.preview_image_link to the Document content type, remove the normal preview_image field to avoid confusions
2.- Add an image
3.- Select the image in the Preview Image field
4.- Save

The serialization:

{
  "@id": "http://localhost:3000/de/page/halfdome2022.jpg",
  "@type": "Image",
  "Subject": [],
  "UID": "e20b8a24ffcd49eeb2b90c47df347686",
  "description": "",
  "effective": "1969-12-30T22:00:00+00:00",
  "image_field": null,
  "image_scales": null,
  "review_state": null,
  "title": "halfdome2022.jpg",
  "type_title": "Bild"
}

The serializer seems to be passing the PersistentDict of the scales to the index and the metadata fields are present in the catalog for that object.
However, when the summary serializer kicks in, the value somehow is null for both. This is correct and being called:

https://github.com/plone/plone.volto/blob/main/src/plone/volto/summary.py#L9-L10

/cc @davisagli @ericof

@sneridagh
Copy link
Member Author

sneridagh commented Apr 1, 2024

We have custom serializers for this in every single project, even in collective.blog. Let's settle on one way of doing it. This has to be in line of what Image component expects. I'm starting to put order in there, see: plone/volto#5939

TBH, I implemented that under the assumption that the serialization that collective.blog was doing was the plone.volto's default... This is to return the whole image object... could be that's overkill, we have to discuss it, but at least is consistent and something that would be "expectable". Something like the summary for me is not.

This is the type:

export interface PossibleImageFields {
  image?:
    | PreviewImage
    | {
        image: PreviewImage;
      };
  preview_image?: PreviewImage;
  preview_image_link?: Content & { image: PreviewImage };
}

type PossibleImageFieldsKeys = keyof PossibleImageFields;
type PossibleImageFieldsUnion = PossibleImageFields[PossibleImageFieldsKeys];

// Being the result:
type PossibleImageFieldsUnion = PreviewImage | {
    image: PreviewImage;
} | (Content & {
    image: PreviewImage;
}) | undefined

and the summary would be completely different.

Let's model also all possible responses in TS, this way we will have a better understanding of what's going on, also start unifying things.

@davisagli
Copy link
Sponsor Member

@sneridagh There are a few different cases and I'm a bit confused about which serialization you're talking about here. (Serialization of the image_preview_link field when you get the page from the content service? Serialization of the preview_image_link in the image_scales field when the page's summary serialization appears in a listing? Something else?) Please specify the URL where you got the serialization that you showed in this issue's description.

@sneridagh
Copy link
Member Author

The snippet above is from the page object content serialization, what's inside of preview_image_link field in the main content endpoint.

@sneridagh
Copy link
Member Author

Which seems to me like a summary of the item?

@sneridagh
Copy link
Member Author

@davisagli Btw, it can be easy reproduced in demo.plone.org

@davisagli
Copy link
Sponsor Member

@sneridagh There it comes from the relation field serializer, which uses the summary serializer: https://github.com/plone/plone.restapi/blob/main/src/plone/restapi/serializer/relationfield.py#L23

The summary serializer adapts its context to IContentListingObject. Normally its context is a catalog brain, and image_scales is available from the catalog metadata.

But when it's used by the relation field serializer, the context is a full object, so the IContentListingObject adapter is RealContentListingObject which does not have access to the image scales from the catalog.

For our client project we implemented an override of RealContentListingObject which adds support for getting the image_scales:

@adapter(IImage)
class RealContentListingObjectWithImageScales(RealContentListingObject):
    def image_scales(self):
        # This should be moved into plone.app.contentlisting at some point.
        obj = self.getObject()
        return getMultiAdapter((obj, getRequest()), IImageScalesAdapter)()

Like the comment says, we should backport that to the base class in plone.app.contentlisting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants