Implementing Sitemap Support

Developers perform this task.

If you want your custom content types to be included in sitemaps, the content types must implement an interface that supports the sitemap background tasks.

Step 1: Implement SiteMapItem

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

  • Extend Content.

  • Implement the getSiteMapEntries() method in SiteMapItem.

    This method returns a list of SiteMapEntry objects. A SiteMapEntry object provides URL and other information about a content type instance. The resulting sitemap XML for a content type instance is determined by the SiteMapEntry methods that you set in the implementation.

The following example shows a getSiteMapEntries() implementation.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class MyContentType extends Content implements Directory.Item, SiteMapItem {

   ...

   @Override
   public List<SiteMapEntry> getSiteMapEntries() {
     List<SiteMapEntry> siteMapEntries = new ArrayList<>();
     Site.Static.findAll().forEach(e -> {
         String sitePermalink = as(Directory.ObjectModification.class).getSitePermalink(e);

         if (!StringUtils.isBlank(sitePermalink)) {
             SiteMapEntry siteMapEntry = new SiteMapEntry();
             siteMapEntry.setUpdateDate(getUpdateDate());
             siteMapEntry.setPermalink(sitePermalink);
             siteMapEntries.add(siteMapEntry);
         }
     });
     return siteMapEntries;
   }
}

In the previous snippet—

  • Line 1 indicates that the class implements SiteMapItem as well as Directory.Item (the interface for creating permalinks).
  • Lines 5–19 implement SiteMapItem#getSiteMapEntries().
    • Line 7 creates a SiteMapEntry list. Because a Content-based object can have different permalinks in a multi-site environment, a list is required to capture the different permalinks that may be set on the MyContentType object.
    • Lines 8-9 initiate a loop that iterates all of the cached sites, checking whether a different permalink is assigned to the MyContentType object for each site.
    • Lines 11-16 create a new SiteMapEntry item with the site’s permalink and the MyContentType object’s modification date, then add the item to the list.
    • Line 18 returns the SiteMapEntry list.

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 will accommodate any new content types that you add. However, if you want a custom sitemap configuration, you must implement SiteMapConfig and related interfaces.

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

The following code snippet shows a partial implementation of SiteMapConfig.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class MySiteMapConfig implements SiteMapConfig {

   @Override
   public JobSettings getJobSettings() {
      return new JobSettings() {

         // Check for correct host
         @Override
         public boolean isAllowedToRun() {
             return TaskUtils.isRunningOnTaskHost();
         }

         // Time to run task
         @Override
         public DateTime calculateRunTime(DateTime currentTime) {
             return currentTime.property(DateTimeFieldType.dayOfMonth()).roundFloorCopy();
         }

         // Job identification in log
         @Override
         public String getLabel() {
             return "Sitemap Settings";
         }
      };
   }

   // Additional implemented SiteMapConfig and GlobalSiteMapConfig methods

   ...
}

In the previous snippet—

  • Lines 3–25 implement the getJobSettings() method as an anonymous class.

    As a best practice, implement JobSettings#isAllowedToRun() to call TaskUtils.isRunningOnTaskHost(). This method verifies that the host on which the task will run is the configured default task host.