Multi-site considerations
In a multi-site environment, you may have use cases where queries need to reflect the site context. For example, the Recent Activity widget and search panel query for objects that are owned by the site that the user selects in the dashboard.
The Site class provides methods to set a site’s properties, site owner of an object, and access to an object from other sites. However, as a best practice, administrators should perform these functions in Brightspot. See Creating a site and Assigning permissions to content on multiple sites for more information.
In a multi-site environment, you may have use cases where queries need to reflect the site context. For example, the Recent Activity widget and search panel query for objects that are owned by the site that the user selects in the dashboard.
By default, a query searches for objects across all sites unless otherwise restricted. For example, a visitor requested an article, and you want to display 10 associated articles. You can use a query field in the model, run the query in the view model, and display the results in the view. The view model must retrieve only articles owned by the site the visitor is at, or articles to which non-owning sites have been granted access. To accomplish this filtering, the view model class can use the Site#itemsPredicate method in the query that gets related articles.
Query query = model.getQuery();
Site site = model.as(Site.ObjectModification.class).getOwner();
if (site != null) query.where(site.itemsPredicate
List<Article> relatedArticles = query.select(0,10).getItems();
-
Gets the query from the model, the
Article
object retrieved by the visitor. -
Gets the owner site of the article.
-
Uses the
site.itemsPredicate
method in the query if the article is owned by a user-created site. This method returns a predicate that filters out any articles that are not owned by the site, or articles to which non-owning sites have not been granted access. If the site isnull
, the article is owned by the Global site, which has access to all objects in Brightspot.
-
Executes the query, returning the first 10 articles retrieved.