Skip to main content

Implementing sitemaps for custom content types

To generate sitemap entries for custom content types, ensure those types implement Directory.Item, SiteMapItem, and optionally SiteMapConfig.

Step 1: Implement SiteMapItem

For a custom content type to be included in a sitemap, it must:

  • Extend Content.
  • Implement the method SiteMapItem#getSiteMapEntries. This method returns a list of SiteMapEntry objects. A SiteMapEntry object provides a URL and other information about an asset. The resulting sitemap XML for an asset is determined by the SiteMapEntry methods that you set in the implementation.

The following example shows a getSiteMapEntries() implementation.

showLineNumbers
1
public class MyContentType extends Content implements
2
Directory.Item, /* Generates permalinks used in sitemaps. */
3
SiteMapItem { /* Generates a list of sitemap entries. */
4
5
@Override
6
public List<SiteMapEntry> getSiteMapEntries() {
7
List<SiteMapEntry> siteMapEntries = new ArrayList<>();
8
9
/*
10
* Loop over all sites hosting this object. If the site
11
* itself has a permalink, create a sitemap entry for the object.
12
*/
13
Site.Static.findAll().forEach(e -> {
14
15
String sitePermalink = as(Directory.ObjectModification.class).getSitePermalink(e);
16
17
if (!StringUtils.isBlank(sitePermalink)) {
18
SiteMapEntry siteMapEntry = new SiteMapEntry();
19
siteMapEntry.setUpdateDate(getUpdateDate());
20
siteMapEntry.setPermalink(sitePermalink);
21
siteMapEntries.add(siteMapEntry);
22
}
23
});
24
25
/* Return the list of sitemap entries associated with this object. */
26
return siteMapEntries;
27
}
28
}

For content types that you want to include in the news- or video-sitemap, implement the NewsSiteMapItem or VideoSiteMapItem interface.

Step 2 (optional): Implement SiteMapConfig

Sitemap configuration informs the background tasks about the types of sitemaps to generate and when to generate them. Brightspot provides a default sitemap configuration that accommodates any custom content types that you add; however, if you want a custom sitemap configuration, you must implement SiteMapConfig and related interfaces.

The SiteMapConfig is a subinterface of GlobalSiteMapConfig, which includes the getJobSettings() method. Implementing this method requires that you implement the JobSettings interface.

The following snippet shows a partial implementation of SiteMapConfig.

showLineNumbers
1
public class MySiteMapConfig implements SiteMapConfig {
2
3
@Override
4
public JobSettings getJobSettings() {
5
return new JobSettings() {
6
7
/*
8
* Check for correct host. As a best practice, implement this method to call
9
* TaskUtils.isRunningOnTaskHost(). Doing so verifies that the host on which
10
* the sitemap generation task will run is the host configured in
11
* Admin > Sites & Settings > Global > Debug > Default Task Host.
12
*/
13
@Override
14
public boolean isAllowedToRun() {
15
return TaskUtils.isRunningOnTaskHost();
16
}
17
18
/* Time to run task. */
19
@Override
20
public DateTime calculateRunTime(DateTime currentTime) {
21
return currentTime.property(DateTimeFieldType.dayOfMonth()).roundFloorCopy();
22
}
23
24
/* Job identification in log. */
25
@Override
26
public String getLabel() {
27
return "Sitemap Settings";
28
}
29
};
30
}
31
32
/* Additionally implemented SiteMapConfig and GlobalSiteMapConfig methods */
33
}