Permalinks
Brightspot provides APIs for managing URLs and generating permalinks automatically.
Brightspot provides the Directory class for managing URL paths to Content
-based objects stored in the database. Directory
and its inner classes provide several APIs to support URL pathing.
Use Directory.Static.hasPathPredicate
to verify that an object has a URL path, for example:
Query query = Query.from(Article.class).where("_id = '00000161-d80b-ddd6-a37d-fc3bb47a0000'");
Article article = (Article) query.where(Directory.Static.hasPathPredicate()).first();
if (article == null) { /* Set a URL path */ }
Use Directory.ObjectModification#addPath
to set a URL path on an object, for example:
Article article = new Article();
article.setHeadline("Hurricane-force Winds on Mt. Washington");
String path = StringUtils.toNormalized(article.getHeadline());
article.as(Directory.ObjectModification.class).addPath(path, Directory.PathType.PERMALINK);
In the above snippet, the addPath
method takes parameters for the URL path and the path type. The path is formed from the headline of the article, and the path type is specified with the Directory.PathType
enumeration. PERMALINK
is a link that never expires. See Content URL types for a description of all URL options.
An object can have different paths for different sites. The addPath
method creates a path for the site that owns the object. If you have multiple sites, you can use the addSitePath
method to create a path that is associated with a specified site.
The following code extends the above example, adding the path before the object is saved. The addSitePath
method creates a path to the object for a second site, News.
@Override
public void beforeSave() {
List<Site> sites = Query.from(Site.class).selectAll();
for (Site site : sites) {
if (site.getName().equals("News")) {
String path = StringUtils.toNormalized(this.getHeadline());
this.as(Directory.ObjectModification.class)
.addSitePath(site, "news//" + path, Directory.PathType.PERMALINK);
break;
}
}
}
The URLs widget in an object’s content edit page shows assigned URL paths. For the article example, there is a path for the owner site, Global, and for a second site, News.
Brightspot provides the Directory.Item#createPermalink method to automatically generate permalinks for a specified site. For example, the following method implementation in the Article
class creates a permalink for each instance of the class, based on the instance headline.
@Override
public String createPermalink(Site site) {
return StringUtils.toNormalized(getHeadline());
}
Implementing createPermalink
in a class is reflected in Brightspot. When you create an instance of the class, the Generate Permalink option appears in the URLs widget on the content edit page. Continuing with the Article
class example, the permalink shown in the widget is based on a user-entered headline for the article.