Hi
I would like to find out programatically if a page with Vanity was called.
This is what I tried:
public boolean getVerifyResourceVanity(Resource resource) {
if (resource == null) {
return false;
}
final String[] vanities = resource.getValueMap().get(VANITY_PROPERTY, new String[0]);
if (vanities.length == 0) {
return false;
}
final Matcher matcher = PREFIX_PATTERN.matcher(resource.getPath());
if (!matcher.find()) {
return false;
}
return true;
}
This information would help me disable a disclaimer when a page with Vanity was called.
Thanks.
Solved! Go to Solution.
You can try
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.ValueMap;
// Assuming you have the Page object
Page page = // Obtain the Page object here
// Get the page's properties
ValueMap properties = page.getProperties();
// Get the cq:vanityUrl property (which can be single or multivalued)
String[] vanityUrls = properties.get("cq:vanityUrl", String[].class);
if (vanityUrls != null && vanityUrls.length > 0) {
for (String vanityUrl : vanityUrls) {
System.out.println("Vanity URL: " + vanityUrl);
}
} else {
System.out.println("No vanity URLs configured for this page.");
}
@anasustic AEM Pagemanager API already have method to fetch vanity url. Please check and also try to use highest abstraction level api where possible AEM > Sling > JCR
Views
Replies
Total Likes
Thank you so much for your answer @Shashi_Mulugu .
The getVanityUrl() method in AEM Pagemanager API returns a String but as you can see in AEM -> Page Property -> Vanity one can configure an array of strings for Vanities.
That is why I used
final String[] vanities = resource.getValueMap().get(VANITY_PROPERTY, new String[0]);
Views
Replies
Total Likes
You can try
import com.day.cq.wcm.api.Page;
import org.apache.sling.api.resource.ValueMap;
// Assuming you have the Page object
Page page = // Obtain the Page object here
// Get the page's properties
ValueMap properties = page.getProperties();
// Get the cq:vanityUrl property (which can be single or multivalued)
String[] vanityUrls = properties.get("cq:vanityUrl", String[].class);
if (vanityUrls != null && vanityUrls.length > 0) {
for (String vanityUrl : vanityUrls) {
System.out.println("Vanity URL: " + vanityUrl);
}
} else {
System.out.println("No vanity URLs configured for this page.");
}
Views
Likes
Replies
Views
Likes
Replies