enonic / xp

Enonic XP
https://enonic.com
GNU General Public License v3.0
202 stars 34 forks source link

Content Lib - Image Component - Return image reference #7284

Closed abdavid closed 5 years ago

abdavid commented 5 years ago

Missing image component type in ComponentMapper class.

I came over this issue when adding an image component to a simple page controller and wanting to use the image id to generate an image url.

First I verified with Data Toolbox that the image was actually there containing an id with reference to the image.

Secondly I used contentLib.query() and contentLib.get() to verify that the image id is missing from the result;

Actual

"page": {
        "type": "page",
        "path": "/",
        "descriptor": "com.somesite.someapp",
        "config": {},
        "regions": {
            "main": {
                "components": [
                    {
                        "path": "/main/0",
                        "type": "image"
                    }
                ]
            }
        }
    },

Expected

"page": {
        "type": "page",
        "path": "/",
        "descriptor": "com.somesite.someapp:default",
        "config": {},
        "regions": {
            "main": {
                "components": [
                    {
                        "path": "/main/0",
                        "type": "image",
                        "image": {
                             "id": "image-id"
                        } 
                    }
                ]
            }
        }
    },

I then started poking around in the content-lib code and found the ComponentMapper class, and from what I can see this class i responsible for mapping a Component -- But I cannot see any references to how Image components are handled which in turn also might explain why I am missing it in the result set.

-- David

alansemenov commented 5 years ago

Hi, and thanks for reporting this. We'll check why image property is not available on the content level. As a workaround, you can retrieve it on the node level, like this (assuming you know contentId and component path inside content):

var nodeLib = require('/lib/xp/node');
var repo = nodeLib.connect({
    repoId: 'com.enonic.cms.default',
    branch: 'draft'
});

var node = repo.get(contentId);

log.info(JSON.stringify(node, null, 4));

if (node && node.components) {

    var imageObject = node.components.filter(function (component) {
        return component.type == 'image' && component.path == '/main/0';
    })[0];

    log.info(JSON.stringify(imageObject.image, null, 4));
}

This will give you an object with id and caption.

abdavid commented 5 years ago

@alansemenov Thanks for the swift reply! this is really why I keep lobbying (among other things) for XP!

I found another work around - I just created a part with a simple ImageSelector and instead of adding the base Image component I just add my custom Image part though I would rather prefer being able to use the "native" Image component as it is confusing for editors :)

alansemenov commented 5 years ago

I'd actually recommend inserting an image via Text component - this would give you more opportunities for image styling via custom styles (where you can combine server-side processing with CSS) plus being able to change alignment, add caption etc. Image references will be extracted on save and available in the content object where you could easily retrieve them from.

But whatever works for you, we'll still look at this bug.

abdavid commented 5 years ago

I am actually having issues with the text component aswell. Running contentstudio 2.0.0. Looks like issues with ckeditor and locales.

  1. aug. 2019 kl. 10:51 skrev Alan Semenov notifications@github.com:

I'd actually recommend inserting an image via Text component - this would give you more opportunities for image styling via custom styles (where you can combine server-side processing with CSS) plus being able to change alignment, add caption etc. Image references will be extracted on save and stored in the content object where you could easily retrieve them from.

But whatever works for you, we'll still look at this bug.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

alansemenov commented 5 years ago

It's probably this one. Already fixed and will be released in Content Studio 2.0.2.

GlennRicaud commented 5 years ago

Thank you for reporting this issue. This will be released with content library 7.0.3 and 7.1.0