Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • npack 58 posts 334 karma points
    Aug 22, 2024 @ 21:50
    npack
    0

    v14.2.0 - How to get Media Url in Typescript from Media Key (Backoffice)?

    Back in v13 angularJs to render a media image in a custom block view we used the 'mediaItemResolver' filter something like this:

    {{mediaItem = (block.data.myMediaProperty[0].mediaKey | mediaItemResolver); ""}}
    
    <img src="{{mediaItem.mediaLink}}" />
    

    Now that we have custom block views in v14.2.0 (yay!) I'm scrambling to recreate some custom block views as extensions with typescript and they're working great except for media-images but I'm not sure what the equivalent would be in v14-land.

    Does anyone know if there is a typescript umbraco utility to convert media keys to url paths?

  • Stefan Stankovic 17 posts 154 karma points
    17 days ago
    Stefan Stankovic
    100

    Hi, This is my implementation:

    import { html, customElement, LitElement, property } from '@umbraco-cms/backoffice/external/lit';
    import { UmbElementMixin } from '@umbraco-cms/backoffice/element-api';
    import type { UmbBlockEditorCustomViewElement } from '@umbraco-cms/backoffice/block-custom-view';
    import { type UmbBlockDataType } from '@umbraco-cms/backoffice/block';
    import { UmbMediaUrlRepository } from '@umbraco-cms/backoffice/media';
    import {Task} from '@lit/task';
    
    // UmbBlockDataType
    @customElement('image-component')
    export class ImageBlockCustomView extends UmbElementMixin(LitElement) implements UmbBlockEditorCustomViewElement {
    
        @property({ attribute: false })
        content?: UmbBlockDataType;
        #itemRepository = new UmbMediaUrlRepository(this);
    
        private _productTask = new Task(this, {
            task: async ([desktopImage]) => {
                const img: {mediaKey: string}[] = desktopImage as {mediaKey: string}[];
                const { data: items } = await this.#itemRepository.requestItems([img[0].mediaKey]);
    
                if (items === undefined || items.length === 0 || items[0].url === undefined) { 
                    throw new Error(`Media doesn't exist: ${img[0].mediaKey}`); 
                }
    
              return items[0].url;
            },
            args: () => [this.content?.desktopImage]
          });
    
        override render() {
            return this._productTask.render({
                initial: () => html`<p>Waiting to start task</p>`,
                pending: () => html`<p>Loading media...</p>`,
                complete: (url) => html`
                <figure class="image-container">
                    <img src="${url}?rmode=min&width=1440&format=webp&quality=80" class="img-responsive" />
                </figure>
                `,
                error: (e) => html`<p>Error: ${e}</p>`
            });
        }
    }
    
    export default ImageBlockCustomView;
    
  • npack 58 posts 334 karma points
    17 days ago
    npack
    0

    Thank you so much, Stefan.

    After bringing in pieces from your implementation I was able to get images to show up in my backoffice block views!

    I appreciate the help -- thanks for taking the time to respond.

Please Sign in or register to post replies

Write your reply to:

Draft