As some of you already know, I’m busy updating some packages from Umbraco 13 to 15 and I have arrived at my next challenge; I need to show a workspace view conditionally depending if the page implements a specific composition.
Creating a custom condition wasn’t very hard, but I found it difficult to check if the current page implements the composition. Besides that, I think there is a bug as well, but I wanted to get your opinion before submitting a bug report.
Getting the current content type is easy:
this.consumeContext(UMB_DOCUMENT_WORKSPACE_CONTEXT, (instance) => {
const contentTypeKey = instance.getContentTypeUnique();
if (!contentTypeKey) {
this.permitted = false;
return;
}
}
Next I get the details of the current content type:
this.consumeContext(UMB_DOCUMENT_TYPE_DETAIL_STORE_CONTEXT,
(storeInstance) => {
const contentType = storeInstance.getItems([contentTypeKey])[0];
}
This content Type has a list of compositions keys, but no more details.
const compositionUniques = contentType?.compositions
.map(composition => composition.contentType.unique);
It’s too bad I only get keys, because I need to check the alias of the composition. So I need to perform additional logic to get the composition itself before I can get to check the alias. This feels a bit much work for something that should be so simple.
Also, this is where the bug comes in. When dealing with content types, there are two functions on the document type details context you can use:
//This gets an array UmbDocumentTypeDetailsModel
const contentType = storeInstance.getItems([contentTypeKey]);
//This gets an observable that ultimately gets a
// UmbDocumentTypeDetailsModel as well
const contentType= storeInstance.byUnique(contentTypeKey)
HOWEVER, when I plug in my unique key of my composition, the getItems() function always returns an empty array, but the byUnique function does work as expected.
//This list contains in my case two keys
const compositionUniques = contentType?.compositions.map(composition => composition.contentType.unique);
//This list is always empty!
const compositions = storeInstance.getItems(compositionUniques);
//However, this works!
const expirationContentType = storeInstance.byUnique(compositionUniques [0]);
expirationContentType.subscribe((documentType) => {
console.log(documentType?.alias);
});
I would expect the getItems to also work on compositions, because I now have to use the observable to make my custom condition work, which is not ideal.
So:
- Is there a easier way to check if the current page implements a composition
- So you think it’s a bug that getItems() doesn’t seem to work with compositions?