TransWikia.com

Registering view for MediaItem (DXA 2.0.3, Java)

Tridion Asked on September 30, 2021

We have a Download entity which extends MediaItem. The download link is used inside RichText component.
This is how the class looks like:

@Data
public class Download extends MediaItem {

    @Override
    public HtmlElement toHtmlElement(String s) {
        return null;
    }

    @Override
    public HtmlElement toHtmlElement(String s, double v, String s1, int i) {
        return null;
    }

    @Override
    public HtmlElement toHtmlElement(String s, double v, String s1, int i, String s2) {
        return null;
    }

    @Override
    public MvcData getDefaultMvcData() {
        return MvcDataCreator.creator()
                .fromQualifiedName("basis:Entity:DownloadLink")
                .defaults(DefaultsMvcData.ENTITY)
                .create();
    }
}

We also have have a RichText component wrapper component with RichText as part of its variable.
My questions are:

  • Why do I need to declare my view inside getDefaultMvcData()? I see that YoutubeVideo from dxa-core:2.0.3 entity also use the same structure. Other entity registrations are done in module initializer (@RegisteredViewModel notation), but why does it have to differ for Media Item entities?
  • What is the purpose of toHtmlElement()? In my case it seems not being used at all, as it uses the view registered in getDefaultMvcData()

One Answer

Q: Why do I need to declare my view inside getDefaultMvcData()?

A: This method is intended for so-called embedded Entity Models. That is, Entity Models which are used within Rich Text content. On the CM-side, these are just linked Components, so these Entity Models won't get MVC data (View name etc.) from CM. If the Entity Model implements this method, it will still be possible to render it like other Entity Models (which do get their MVC data from CM). That is: using the <dxa:entity> and <dxa:entities> tags.

Q: What is the purpose of toHtmlElement()?

A: These methods are specific for MediaItem-derived View Model classes. There are use cases where MediaItem-derived View Models are rendered to HTML directly without using a View. In particular: if you use the <dxa:media> tag.

I was about to say: check the implementation/documentation of the Download View Model in the Core Module, but I see it's not really well documented. The .NET variant has better docs.

Correct answer by Rick Pannekoek on September 30, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP