Integrating Context with a Property Editor
Integrate one of the built-in Umbraco Contexts.
Overview
This is the third step in the Property Editor tutorial. This part shows how to integrate built-in Umbraco Contexts. For this sample, let us use the UmbNotificationContext for some pop-ups and the UmbModalManagerContext. UmbNotificationContext shows a notification when the Trim button is clicked and the input exceeds the maxChars limit.
This part covers:
Setting up the contexts
Add the following imports in the
suggestions-property-editor-ui.element.tsfile. This includes the notification context.
import { UMB_NOTIFICATION_CONTEXT, UmbNotificationContext, type UmbNotificationDefaultData } from '@umbraco-cms/backoffice/notification';
import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';Update the class to extend from UmbElementMixin. This allows to consume the contexts needed:
export default class MySuggestionsPropertyEditorUIElement extends UmbElementMixin((LitElement)) implements UmbPropertyEditorUiElement {
Create the constructor to consume the notification context above the
render()method:
#notificationContext?: UmbNotificationContext;
constructor() {
super();
this.consumeContext(UMB_NOTIFICATION_CONTEXT, (instance) => {
this.#notificationContext = instance;
});
}Using the notification context
With the notification context in place, update the #onTrimText method.
Check whether the input length is less than or equal to the maxChars value. If so, a notification is shown to indicate there is nothing to trim.
Use the NotificationContext's peek method here. The method has two parameters: an UmbNotificationColor and an UmbNotificationOptions object. Pass an UmbNotificationDefaultData object in the data property of the options. The message property is required.
Add the
#onTextTrim()method above therender()method:
Add a
clickevent to the trim text button in therender()method:
If the input length is less or equal to the maxLength configuration, a notification is displayed when clicking on the Trim button.

Adding more logic to the context
Continue by adding more logic. If the length exceeds maxChars, show a confirmation dialog before trimming.
Here, use the ModalManagerContext which has an open method to show a dialog.
As with the notification context, import it and consume it in the constructor.
Add the following import in the
suggestions-property-editor-ui.element.tsfile:
Update the notification import to remove the
UmbNotificationContexttype, which is no longer needed:
Update the constructor to consume the
UMB_MODAL_MANAGER_CONTEXTand theUMB_CONFIRM_MODAL.
Add more logic to the
onTextTrimmethod:
The content of the confirm modal is a Lit html template, so Lit escapes the trimmed value. The confirm modal renders a string content as HTML, so never interpolate user input into a string.
Run the command
npm run buildin thesuggestionsfolder.Run the project.
Go to the Content section of the Backoffice.
Ask for suggestions and click on the Trim text button. If the suggested text is long enough to be trimmed, you will be asked for confirmation:

Enforcing the Character Limit
To enforce the limit properly, your property editor needs to be a form control.
Making the editor a Form Control
Adding a character counter
With all of these in place, the editor will:
Show a live character counter.
Display a validation message when the limit is exceeded.
Block saving or publishing until the value is within the limit.

Wrap up
Over the previous steps, the following was covered:
Created a plugin.
Defined an editor.
Registered the Data Type in Umbraco.
Added configuration to the Property Editor.
Connected the editor with
UmbNotificationContextandUmbModalManagerContext.Looked at some of the methods from notification & modal manager contexts in action.
Integrated one of the built-in Umbraco Contexts with the Property Editor.
Enforced the
maxCharsconfiguration usingUmbFormControlMixin, including a live character counter and validation that blocks saving and publishing.
Last updated
Was this helpful?