use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class RenderComponentDirective method getComponentFromNode.
protected SiteItem getComponentFromNode(TemplateModel parentParam, TemplateModel componentParam, Environment env) throws TemplateException {
SiteItem parentItem = unwrap(COMPONENT_PARENT_PARAM_NAME, parentParam, SiteItem.class, env);
Element includeElementParent = unwrap(COMPONENT_PARAM_NAME, componentParam, Element.class, env);
Element includeElement = includeElementParent.element(includeElementName);
Element componentElement = includeElementParent.element(componentElementName);
if (includeElement != null) {
logger.debug("Using the include element to load the site item");
String componentPath = includeElement.getTextTrim();
return getComponent(componentPath, env);
} else if (componentElement != null) {
logger.debug("Using an embedded site item");
if (parentItem == null) {
logger.debug("Using default parent component");
parentItem = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL), SiteItem.class, env);
}
return siteItemService.getSiteItem(parentItem, componentElement);
} else {
throw new IllegalStateException("No '" + includeElementName + "' or '" + componentElementName + "' element " + "found under component " + includeElementParent.getUniquePath());
}
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class RenderComponentDirective method getComponent.
protected SiteItem getComponent(String componentPath, Environment env) throws TemplateException {
SiteItem currentPage = unwrap(KEY_CONTENT_MODEL, env.getVariable(CrafterPageView.KEY_CONTENT_MODEL), SiteItem.class, env);
if (currentPage != null) {
String basePath = FilenameUtils.getFullPath(currentPage.getStoreUrl());
URI baseUri = URI.create(basePath);
try {
componentPath = baseUri.resolve(componentPath).toString();
} catch (IllegalArgumentException e) {
throw new TemplateException("Invalid relative component URL " + componentPath, e, env);
}
}
SiteItem component;
try {
component = siteItemService.getSiteItem(componentPath);
} catch (Exception e) {
throw new TemplateException("Unable to load component " + componentPath + " from the underlying repository", e, env);
}
if (component == null) {
throw new TemplateException("No component found at path '" + componentPath + "'", env);
}
return component;
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class NavBreadcrumbBuilderImpl method getBreadcrumb.
@Override
public List<NavItem> getBreadcrumb(String url, String root, Converter<SiteItem, NavItem> itemConverter) {
if (itemConverter == null) {
itemConverter = defaultItemConverter;
}
List<NavItem> breadcrumb = new ArrayList<>();
String breadcrumbUrl = extractBreadcrumbUrl(url, root);
String[] breadcrumbUrlComponents = breadcrumbUrl.split("/");
String currentUrl = root;
for (String breadcrumbUrlComponent : breadcrumbUrlComponents) {
currentUrl = UrlUtils.concat(currentUrl, breadcrumbUrlComponent);
SiteItem siteItem = siteItemService.getSiteItem(currentUrl, processor);
if (siteItem != null) {
NavItem navItem = itemConverter.convert(siteItem);
if (navItem != null) {
navItem.setActive(url.equals(siteItem.getItem().getDescriptorUrl()));
breadcrumb.add(navItem);
}
}
}
return breadcrumb;
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class NavTreeBuilderImplTest method getSiteItemService.
private SiteItemService getSiteItemService() {
SiteItemService siteItemService = mock(SiteItemService.class);
SiteItem item1 = createSiteItem(URL1);
SiteItem item3 = createSiteItem(URL3);
SiteItem item2 = createSiteItem(URL2, item3);
SiteItem rootItem = createSiteItem(ROOT_URL, item1, item2);
when(siteItemService.getSiteTree(ROOT_URL, -1, (ItemFilter) null, null)).thenReturn(rootItem);
return siteItemService;
}
use of org.craftercms.engine.model.SiteItem in project engine by craftercms.
the class NavTreeBuilderImplTest method getItemConverter.
private Converter<SiteItem, NavItem> getItemConverter() {
Converter<SiteItem, NavItem> converter = mock(Converter.class);
doAnswer((Answer<NavItem>) invocation -> {
SiteItem siteItem = (SiteItem) invocation.getArguments()[0];
NavItem navItem = new NavItem();
navItem.setLabel(siteItem.getStoreName());
navItem.setUrl(siteItem.getStoreUrl());
return navItem;
}).when(converter).convert(any(SiteItem.class));
return converter;
}
Aggregations