I’m using the Umbraco 17 beta and I have a working backoffice custom view for a block. I can render data from the block. I’m using the Lit/TypeScript/Vite method.
How do I get the name of the current content node? I assume it’s by consuming one of the contexts, but I’ve been consuming them and using console.log to explore the objects, and I can’t find it anywhere.
This is the kind of thing I’m doing just to try to work it out:
If it’s for a block that you are editing in the context of a content node, you need UMB_CONTENT_WORKSPACE_CONTEXT:
import { UMB_CONTENT_WORKSPACE_CONTEXT } from '@umbraco-cms/backoffice/content';
this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, (context) => {
//If you just want it one-time
//This does not react to name changes
const name = context.getName()
//If you want to be kept up to date of changes, you need to
//observe it!
this.observe(context.name(), (name) => {
//This is called every time the name changes
});
}
Thank you! The one-time method is working, and that is good enough for my use case.
Observing changes is a nice-to-have though, plus I’d like to understand the new model better. That part isn’t working. The callback runs when the content is loaded in the backoffice but the name variable is blank. If I change the node name it doesn’t run again.
I may add one detail here, the Workspace manages the Document as a whole. Which is why name is a method, the argument for it is the variant-id. Meaning this works if your Document does not vary (because no argument is begin seen as targeting the invariant-variant, which only exists for not varying documents — that is how it was suppose to work, I just pushed a fix for this.)
But if it varies this will not give you anything.
Instead of talking to the Workspace, you should talk to the Property Dataset (UMB_PROPERTY_DATASET_CONTEXT) but this is also available locally for the Block.
So the way to handle it would be by consume a more specific Token(UMB_DOCUMENT_PROPERTY_DATASET_CONTEXT) and allow it to pass matches:
So this would be the solution for you in that case:
import { UMB_DOCUMENT_PROPERTY_DATASET_CONTEXT } from '@umbraco-cms/backoffice/document';
this.consumeContext(UMB_DOCUMENT_PROPERTY_DATASET_CONTEXT, (context) => {
this.observe(context.name, (name) => {
// there you have the name and this is called everytime the name updates.
});
}.passContextAliasMatches();
Notice this is also how you would get the active culture for your scope. (via the .ctulreu property) and also the way to get the value of other properties on this variant.