Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Injecting currentPage using annotations does not return the correct page

Avatar

Level 4

https://github.com/adobe/aem-sample-we-retail-journal/issues/53

 

shyamasundarks_2-1666243005154.png

The same issue I was facing Breadcrumb was working fine in my local but UAT was not working I followed the above link and fixed the issue it worked in the UAT server. But once deploy to the Production server it is not working.

 

My code:

Page currentPage = request.getResourceResolver().adaptTo(PageManager.class)
.getContainingPage(request.getResource());

Instead of 

@ScriptVariable
private Page currentPage;

Before this fix: It was showing child pages of the root page. All pages were displayed irrespective of the hideInNavigation property.

 /content/page1/page2/page3/page4/page5

After Fix:

page3/page4/page5

 

It solved the problem, but it is working fine QA/UAT server but in production server not working.

In Production: Displaying all the pages

/content/page1/page2/page3/page4/page5

 

Please can help me with this issue?

 

Details:

AEM-SPA Angular Project

AEM Version: 6.5

 


import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Exporter;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.injectorspecific.OSGiService;
import org.apache.sling.models.annotations.injectorspecific.ScriptVariable;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.apache.sling.settings.SlingSettingsService;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.adobe.cq.export.json.ComponentExporter;
import com.adobe.cq.export.json.ExporterConstants;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.designer.Style;

@Model(adaptables = SlingHttpServletRequest.class, adapters = { BreadcrumbModel.class,
ComponentExporter.class }, resourceType = { BreadcrumbModel.RESOURCE_TYPE })
@exporter(name = ExporterConstants.SLING_MODEL_EXPORTER_NAME, extensions = ExporterConstants.SLING_MODEL_EXTENSION)
public class BreadcrumbModel implements ComponentExporter {

protected static final String RESOURCE_TYPE = "projectA/components/breadcrumb";
String PN_SHOW_HIDDEN = "showHidden";
String PN_HIDE_CURRENT = "hideCurrent";
String PN_START_LEVEL = "startLevel";

protected static final boolean PROP_SHOW_HIDDEN_DEFAULT = false;
protected static final boolean PROP_HIDE_CURRENT_DEFAULT = false;
protected static final int PROP_START_LEVEL_DEFAULT = 2;

private final transient Logger logger = LoggerFactory.getLogger(getClass());

@ScriptVariable
private ValueMap properties;

@ScriptVariable
private Style currentStyle;

@OSGiService
private SlingSettingsService settingsService;

@Deleted Account
private SlingHttpServletRequest request;

private boolean showHidden;
private boolean hideCurrent;
private int startLevel;

@PostConstruct
private void initModel() {
startLevel = properties.get(PN_START_LEVEL, currentStyle.get(PN_START_LEVEL, PROP_START_LEVEL_DEFAULT));
showHidden = properties.get(PN_SHOW_HIDDEN, currentStyle.get(PN_SHOW_HIDDEN, PROP_SHOW_HIDDEN_DEFAULT));
hideCurrent = properties.get(PN_HIDE_CURRENT, currentStyle.get(PN_HIDE_CURRENT, PROP_HIDE_CURRENT_DEFAULT));
}

public String getJsonVal() {
String jsonVal = "";
try {
jsonVal = createItems().toString();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonVal;
}

private JSONArray createItems() throws JSONException {
JSONArray items = new JSONArray();
Page currentPage = request.getResourceResolver().adaptTo(PageManager.class)
.getContainingPage(request.getResource());

logger.info("Breadcrumb pagepath= " + currentPage.getPath());
int currentLevel = currentPage.getDepth();
while (startLevel < currentLevel) {
Page page = currentPage.getAbsoluteParent(startLevel);
if (page != null) {
boolean isActivePage = page.equals(currentPage);
if (isActivePage && hideCurrent) {
break;
}
if (checkIfNotHidden(page)) {
JSONObject item = new JSONObject();
item.put("active", isActivePage);
item.put("title", getTitle(page));
if (!isAuthor()) {
item.put("url", getShortURL(getRedirectUrl(page)));
logger.info("short-hand URL path= " + getShortURL(getRedirectUrl(page)));
}else{
item.put("url", getRedirectUrl(page));
}
items.put(item);
}
}
startLevel++;
}
return items;
}

private String getTitle(Page page) {
return StringUtils.isEmpty(page.getNavigationTitle())
? StringUtils.isEmpty(page.getTitle()) ? page.getName() : page.getTitle()
: page.getNavigationTitle();
}

private String getRedirectUrl(Page page) {

String url = page.getProperties().containsKey("redirectTarget")
&& StringUtils.isNotEmpty(page.getProperties().get("redirectTarget").toString())
? page.getProperties().get("redirectTarget").toString()
: page.getPath();
return url.endsWith(".html") ? url : url + ".html";
}

private boolean checkIfNotHidden(Page page) {
return !page.isHideInNav() || showHidden;
}

@Override
public String getExportedType() {
return RESOURCE_TYPE;
}

private boolean isAuthor() {
return settingsService.getRunModes().contains("author");
}

private String getShortURL(String path) {
// String shorturl = request.getResourceResolver().map(path);
String shorturl = "";

if (path != null) {

String[] patharray = path.split("/");

for (int i = 4; i < patharray.length; i++) {

shorturl = shorturl + "/" + patharray[i];
}

}
logger.info("shortt URL= " + shorturl);

return shorturl;
}

}

9 Replies

Avatar

Community Advisor

I think, you should check the core component version. In case you have different version of core component or AEM in local.



Arun Patidar

Avatar

Level 4

@arunpatidar : I verified in all the environments, Local/QA/UAT/PRD: It's having 

core.wcm.components.content-2.7.0.zip

JSON response in PRD is different. It is giving root-page and its children for all the pages.

For example:

In Production: Displaying all the pages

/content/page1/page2/page3/page4/page5

Should display only 

page3/page4/page5

because all are hideinNavigation property is enabled true won't display.

Same issue I am facing as stated in this link

https://github.com/adobe/aem-sample-we-retail-journal/issues/53

 

 It looks like the breadcrumb items are being generated only for the last page in :children property from root model.json. I believe this is the 'last created page'.

shyamasundarks_0-1666274071918.png

Anything about this post, need to connect with the Adobe team? but I don't have the evidence of what is wrong with PRD ?

shyamasundarks_2-1666274396274.png

 

Avatar

Employee Advisor

Do you see any exception in logs regarding this ?

Avatar

Level 4

No Exception but giving the wrong JSON response in PRD.

https://github.com/adobe/aem-sample-we-retail-journal/issues/53

 It looks like the breadcrumb items are being generated only for the last page in :children property from root model.json. I believe this is the 'last created page'.

shyamasundarks_1-1666274124269.png

 

Avatar

Community Advisor

If it works fine on UAT then check version of core components in prod.

Avatar

Level 4

 I verified in all the environments, Local/QA/UAT/PRD: It's having 

core.wcm.components.content-2.7.0.zip

Avatar

Community Advisor

@shyamasundar-ks 

I see you are getting startLevel property from component properties. Can you check the value, if it is configured as 0.

Avatar

Level 4

@Anudeep_Garnepudi: Yes it is configured to 0 on all the pages but it will display pages based on the hideInNavigation property, so starting 3 pages won't display only displays correct behavior. It is working fine in a lower environment.

Avatar

Employee Advisor

The problem is the definition of "currentPage" and how it is calculated.

 

As far I understood, it is always relative to the current resource which this SlingModel is adapted from; that can be the page which is requested, but it could also be any other page; this depends solely on the rendering process and which resources are rendered.