Creating a property value preset, but limited to specific document types

Hi,

Looking into some guidance/advice on whether it’s possible to have a property value preset set for specific properties on some specific document types. From what I can read in the docs ( Property Value Preset | Umbraco CMS ), it’s possible to limit to a specific property type (defined as eg: forPropertyEditorUiAlias: ‘Umb.PropertyEditorUi.TextBox’), but not sure if we can also limit to for example a specific textbox on a specific document type (one can have multiple textboxes for a single document type, but what if i only wanted to have a preset value for one textbox on that one specific document type)?

–Dirk

I was thinking that this might be possible if the propertyValuePreset extension supports conditions. According to AI, it does, so I think I would go in that direction.

I’ll put in an example AI generated for the idea, butI doubt it’s correct. But conditions seem to way to go:

const manifest: ManifestPropertyValuePreset = {
	type: 'propertyValuePreset',
	name: 'My Document Type Specific Preset',
	alias: 'My.PropertyValuePreset.DocumentTypeSpecific',
	api: MyPropertyValuePresetApi,
	forPropertyEditorUiAlias: 'Umb.PropertyEditorUi.TextBox',
	conditions: [
		{
			alias: 'Umb.Condition.WorkspaceContentTypeAlias',
			match: 'blogPost' // Only apply to blogPost document type
			// or use oneOf: ['blogPost', 'newsArticle'] for multiple types
		}
	]
};

Hi Luuk,

Thanks for the reply, I might try that direction, even though i will need probably a custom condition to make sure only one specific property is targeted (One content picker on the document which has multiple content pickers)

I don’t see conditions as valid schema on the ManifestPropertyValuePreset in Umbraco 16.2.0, did you ever get further along with this Dirk?

I haven’t Jesper, because i found out that the legacy code we were using was just not very great… we were setting a property value based on content creation from a parent’t property value… while using fallback is a much better approach (in the specific use case, the child content could potentially override the parent’s value (using the same property alias), so we had a SendingContentNotification to fetch the parent”s value and set it on the child’s property.

While the better approach is to have the same property (alias) on both doc types (one was landing page, other was detail page) and have the detail override the parent’s mandatory default value

But on the other hand, it would really make sense to be able to specify a default value for a specific document type AND specific property (alias)

1 Like

I agree, for now I have created a discussion on the CMS repo where I ask for conditions to be enabled: Add conditions to more extension manifests · umbraco/Umbraco-CMS · Discussion #20390

As if someone was listening… Andy has written an article on how to do prevalues for specific doc types and content types.

Not exactly using conditions but worth a read: https://skrift.io/issues/client-side-backoffice-extensions-with-server-data/

I saw that earlier, and finally figured out how to do it, here is a full example that works for a datetime property editor to check for a specific doctype alias and then preset the current date incase anyone else needs something similar:

import type {
    UmbPropertyTypePresetModelTypeModel,
    UmbPropertyValuePreset
} from "@umbraco-cms/backoffice/property";
import type { UmbPropertyEditorConfig } from "@umbraco-cms/backoffice/property-editor";
import {UmbControllerBase} from "@umbraco-cms/backoffice/class-api";
import type {UmbControllerHost} from "@umbraco-cms/backoffice/controller-api";
import { UMB_DOCUMENT_WORKSPACE_CONTEXT  } from '@umbraco-cms/backoffice/document';
export class DatePrevalue extends UmbControllerBase implements UmbPropertyValuePreset<string, UmbPropertyEditorConfig> {
    private permitted: boolean = false;

    constructor(host: UmbControllerHost) {
        super(host);

        this.consumeContext(UMB_DOCUMENT_WORKSPACE_CONTEXT, (instance) => {
            this.observe(instance?.structure.contentTypeAliases, (aliases) => {
                if(aliases?.includes('downloadItem')) {
                    this.permitted = true;
                } else {
                    this.permitted = false;
                }
            });
        });
    }

    async processValue(value: any, _config: UmbPropertyEditorConfig, _typeArgs: UmbPropertyTypePresetModelTypeModel) {
        if(this.permitted){
            return value ? value : new Date().toISOString().replace('T', ' ').substring(0, 19);
        }
        
        return value;
    }

    destroy(): void {
        super.destroy();
    }
}

export { DatePrevalue as api };
2 Likes