Specify a default value for a content node name (multiple variants)

I’ll follow up with solution i came up with and is working fine:

export class SetNodeNameBasedOnDate extends UmbControllerBase {
  #workspaceContext?: UmbContentWorkspaceContext;
  #currentDate?: string;
  #languages?: Array<UmbLanguageDetailModel>;

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

    this.consumeContext(UMB_CONTENT_WORKSPACE_CONTEXT, (context) => {
      this.#workspaceContext = context;
      this.#setupPropertyWatcher();
    });

    this.consumeContext(UMB_APP_LANGUAGE_CONTEXT, (appLanguageContext) => {
      this.observe(appLanguageContext?.languages, (languages) => {
        this.#languages = languages;
      })
    });
  }

  async #setupPropertyWatcher() {
    if (!this.#workspaceContext) return;

    // Await the property observable
    const dateObservable = await this.#workspaceContext.propertyValueByAlias("date");

    if (dateObservable) {
      this.observe(dateObservable, (value) => {
        setTimeout(() => {
          this.#currentDate = value?.toString();

          this.#updateNodeName(this.#currentDate, this.#languages);
        }, 0); // 0ms delay lets the event loop finish updating the model
      });
    }
  }

  async #updateNodeName(value: string | undefined, languages: UmbLanguageDetailModel[] | undefined) {
    // Returned value from property is either yyyy-MM-dd HH:mm or yyyy-MM-dd HH:mm:ss - Depends on whether seconds fragment of the date was set
    var date = value!.length === 16 ? value + ":00" : (value as string);

    languages!.forEach(language => {
      this.#workspaceContext!.setName(date, UmbVariantId.FromString(language.unique));
    });
  }
}

export default SetNodeNameBasedOnDate ;
1 Like