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

adding source property in sling model of image component to get dam asset path in headless json #1198

Merged
merged 10 commits into from
May 7, 2024
Expand Up @@ -16,7 +16,9 @@
package com.adobe.cq.forms.core.components.internal.models.v1.form;

import java.io.IOException;
import java.util.Map;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.jcr.RepositoryException;

Expand Down Expand Up @@ -45,6 +47,8 @@
extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class StaticImageImpl extends AbstractFormComponentImpl implements StaticImage {

public static final String DAM_REPO_PATH = "fd:repoPath";

private Image image;

@SlingObject
Expand All @@ -62,6 +66,10 @@ public class StaticImageImpl extends AbstractFormComponentImpl implements Static
@org.jetbrains.annotations.Nullable
protected String description; // long description as per current spec

@ValueMapValue(injectionStrategy = InjectionStrategy.OPTIONAL, name = "fileReference")
@Nullable
protected String fileReference;

/**
* Returns the source where the image is present.
*
Expand Down Expand Up @@ -120,4 +128,13 @@ public String getLinkUrl() {
return null;
}
}

@Override
public @Nonnull Map<String, Object> getProperties() {
Map<String, Object> properties = super.getProperties();
if (fileReference != null && fileReference.length() > 0) {
properties.put(DAM_REPO_PATH, fileReference);
}
return properties;
}
}
Expand Up @@ -48,6 +48,8 @@ public class StaticImageImplTest {
private static final String BASE = "/form/image";
private static final String CONTENT_ROOT = "/content";
private static final String PATH_IMAGE_CUSTOMIZED = CONTENT_ROOT + "/image-customized";

private static final String PATH_IMAGE_PARSED = CONTENT_ROOT + "/image-parsedSrc";
private static final String PATH_IMAGE = CONTENT_ROOT + "/image";
private static final String PATH_IMAGE_DATALAYER = CONTENT_ROOT + "/image-datalayer";

Expand All @@ -67,6 +69,13 @@ void testExportedType() {
assertEquals("", staticImageMock.getExportedType());
}

@Test
void testExportedTypeForParsedImage() {
StaticImage staticImage = Utils.getComponentUnderTest(PATH_IMAGE_PARSED, StaticImage.class, context);
assertEquals(FormConstants.RT_FD_FORM_IMAGE_V1, staticImage.getExportedType());
assertEquals("/content/dam/formsanddocuments/abc.jpeg", staticImage.getProperties().get("fd:repoPath"));
}

@Test
void testFieldType() {
StaticImage staticImage = Utils.getComponentUnderTest(PATH_IMAGE_CUSTOMIZED, StaticImage.class, context);
Expand Down
@@ -0,0 +1,18 @@
{
"id": "image-7cfd7f1fe4",
"fieldType": "image",
"name": "abcd",
"visible": false,
"altText": "abc",
"value": "/content/image-customized.img.png",
"events": {
"custom:setProperty": [
"$event.payload"
]
},
"properties": {
"fd:path": "/content/image",
"fd:repoPath": "/content/dam/formsanddocuments/abc.jpeg"
},
":type": "core/fd/components/form/image/v1/image"
}
Expand Up @@ -8,7 +8,8 @@
]
},
"properties": {
"fd:path": "/content/image"
"fd:path": "/content/image",
"fd:repoPath": "/content/dam/formsanddocuments/abc.jpeg"
},
":type": "core/fd/components/form/image/v1/image"
}
13 changes: 13 additions & 0 deletions bundles/af-core/src/test/resources/form/image/test-content.json
@@ -1,4 +1,16 @@
{
"image-parsedSrc" : {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/fd/components/form/image/v1/image",
"name" : "abcd",
"jcr:title" : "def",
"hideTitle" : false,
"visible" : false,
"altText": "abc",
"fileReference": "/content/dam/formsanddocuments/abc.jpeg",
"excludeFromDor": false,
"fieldType": "image"
},
"image-customized" : {
"jcr:primaryType": "nt:unstructured",
"sling:resourceType": "core/fd/components/form/image/v1/image",
Expand All @@ -21,6 +33,7 @@
"jcr:createdBy": "admin",
"jcr:title": "Image",
"name": "emptyImage",
"fileReference": "/content/dam/formsanddocuments/abc.jpeg",
"sling:resourceType": "core/fd/components/form/image/v1/image",
"fieldType": "image"
},
Expand Down