Expand my Community achievements bar.

Can we extend spa react xfpage model so that we can access the custom page properties added in the page component

Avatar

Level 1

I trying to add custom page properties in the xfpage dialog and instead off using propertiy.property name I want to extend the xfpage model and return the properties from the model as json. is there any reference or documentation to achieve this  

5 Replies

Avatar

Community Advisor

Hi,

You need to use the delegation pattern to extend the model that is exporting the data and add your custom data. Then, you can use that data instead of the out-of-the-box (OOTB) data. You can check how to do this here:  https://experienceleague.adobe.com/en/docs/experience-manager-learn/getting-started-with-aem-headle... 

 

Hope this helps



Esteban Bustamante

Avatar

Level 1

Hi @EstebanBustamante ,Thank you for your response. I would like to illustrate that I am currently employing the delegation pattern, as below please let me know if you see anything wrong in this:

package com.project.global.spa.react.core.models.impl;
 
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Via;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.models.annotations.via.ResourceSuperType;
 
import com.adobe.aem.spa.project.core.models.Page;
import com.adobe.cq.export.json.ContainerExporter;
import com.adobe.cq.export.json.ExporterConstants;
 
import lombok.experimental.Delegate;
 
@Model(adaptables = SlingHttpServletRequest.class, adapters = { Page.class,
ContainerExporter.class }, resourceType = projectReactXfPageImpl.RESOURCE_TYPE)
 
@Exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class projectReactXfPageImpl implements Page {
 
static final String RESOURCE_TYPE = "project-global-spa-react/components/xfpage";
 
@Self
private SlingHttpServletRequest request;
 
@Self
@Via(type = ResourceSuperType.class)
private Page delegate;
However, upon reviewing the error.log file, I continue to encounter the following errors:
 
Caused by: org.apache.sling.models.factory.MissingElementsException: Could not inject all required fields into class com.project.global.spa.react.core.models.impl.projectReactXfPageImpl
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:696) [org.apache.sling.models.impl:1.6.5.B0001]
at org.apache.sling.models.impl.ModelAdapterFactory.internalCreateModel(ModelAdapterFactory.java:369) [org.apache.sling.models.impl:1.6.5.B0001]
at org.apache.sling.models.impl.ModelAdapterFactory.adapt(ModelAdapterFactory.java:990) [org.apache.sling.models.impl:1.6.5.B0001]
... 286 common frames omitted
Suppressed: org.apache.sling.models.factory.MissingElementException: Could not inject private com.adobe.aem.spa.project.core.models.Page com.project.global.spa.react.core.models.impl.projectReactXfPageImpl.delegate
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:690) [org.apache.sling.models.impl:1.6.5.B0001]
... 288 common frames omitted
Caused by: org.apache.sling.models.factory.ModelClassException: No injector returned a non-null value!
at org.apache.sling.models.impl.ModelAdapterFactory.injectElementInternal(ModelAdapterFactory.java:598) [org.apache.sling.models.impl:1.6.5.B0001]
at org.apache.sling.models.impl.ModelAdapterFactory.injectElement(ModelAdapterFactory.java:487) [org.apache.sling.models.impl:1.6.5.B0001]
at org.apache.sling.models.impl.ModelAdapterFactory.createObject(ModelAdapterFactory.java:685) [org.apache.sling.models.impl:1.6.5.B0001]
... 288 common frames omitted

Avatar

Community Advisor

Hey, check that actually your project-global-spa-react/components/xfpage inherits from "PAGE" (the resourceType you are adding in the adapter section). Please check this example of how the resourceType and relationship is handled in order for the delegation to work properly: https://github.com/adobe/aem-core-wcm-components/wiki/Delegation-Pattern-for-Sling-Models 

 

Hope this helps



Esteban Bustamante

Avatar

Administrator

@pankajda3 Did you find the suggestions helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni

Avatar

Administrator

@pankajda3 Did you find the suggestion helpful? Please let us know if you require more information. Otherwise, please mark the answer as correct for posterity. If you've discovered a solution yourself, we would appreciate it if you could share it with the community. Thank you!



Kautuk Sahni