Modal context accessing parent context
# package-development
r
Is it possible for a modal context to access a parent context? For example, I'm trying to access
UMB_DOCUMENT_WORKSPACE_CONTEXT
when I'm inside of a block modal (
UMB_BLOCK_WORKSPACE_CONTEXT
)
l
It's not pretty, but the Block Workspace Context has a
modalContext
property, which you can get access to the host element, from which you can get access to the Document Workspace Context. e.g. something like this...
Copy code
const host = blockContext.modalContext.getHostElement();
host.consumeContext(UMB_DOCUMENT_WORKSPACE_CONTEXT, (docContext) => {
    console.log('UMB_DOCUMENT_WORKSPACE_CONTEXT', docContext);
});
Alternatively, if you are only after getting the document's ID, then you could try consuming the
UmbMenuStructureWorkspaceContext
... this is the context for the breadcrumbs in the footer. Feels slight misuse, but that's how I'm using it in Contentment. e.g. https://github.com/leekelleher/umbraco-contentment/blob/dev/v6.x/src/Umbraco.Community.Contentment.Client/src/property-editor-ui/data-picker/data-picker.element.ts#L75-L82
r
Thank you @leekelleher!
n
Hi @rickbutterfield and @leekelleher There is a more proper way to do this, which I think isn't documented. And neither used in any of our code. So no way you would have known. The options is called
passContextAliasMatches
and you go about it in this way:
Copy code
this.consumeContext(MY_SPECIFIC_TOKEN, (context) => {
   ...
}).passContextAliasMatches();
What happens when this is not turned on, is that the request stops at a Context Alias match. This prevents you from accidentally retrieving a parent context of another scope than the one you are interested in. But in this case you know what you are looking for, and you are clearly looking to retrieve something above the current Workspace. If you dig deeper, you will see that all Workspace Contexts uses the same Context Alias, which is why you without this setting, are not able to escape it. So the difference between UMB_WORKSPACE_CONTEXT and these UMB_NAMEABLE_WORKSPACE_CONTEXT, UMB_CONTENT_WORKSPACE_CONTEXT, UMB_BLOCK_WORKSPACE_CONTEXT, UMB_DOCUMENT_WORKSPACE_CONTEXT, + many more. Is that these last four has further specifications to be meet before a match is accepted. This can be used in two ways: Your way, to only get a context that matches your exact needs. And especially in combination with the
passContextAliasMatches
you can pass through other sligtlhy similar contexts to get to it. Another usage could be to use the retrival of a context, to determin the situation your code running from. Example. consuming UMB_BLOCK_WORKSPACE_CONTEXT, without
passContextAliasMatches
would only give a match if you are in a Block Workspace, then you could use that to act differently. We use this our selfs to determine wether a Workspace should get a close button. By consuming the Modal Context we then know if we retrieved one we are inside a modal and will then display the close button.
On that note, you may want to look for
UMB_CONTENT_WORKSPACE_CONTEXT
in this way your code should be able to work when Blocks works on Media and Member. — Assuming what you want to do is supported by that interface. 👍
7 Views