1

In our DXA (2.0, Java) implementation we have a couple of scenarios where there are component links to items that can be from more than one schema.

Firstly we have a generic Link embedded schema (and corresponding Link model) which, can link to both normal and multimedia components. If its a normal component, we show the link URL, but if its a multimedia component (in this case a Download) then we also want to show the file size and an icon for the type by the link. In order to achieve this we added a MediaItem linkedMedia property on the Link model, which works fine if the linked item is indeed a multimedia component, but gives exceptions in the logs if it is not. Is there some way to suppress this exception?

Secondly we give the editor the ability to 'embed' linked content. The linked content can be from one of a number of different normal content or multimedia schemas (image, video, data table etc.). For Multimedia content I can create a MediaItem embeddedMedia property which the DXA will make the appropriate subclass model (Image or Video), but I don't seem to be able to do the same with an abstract EmbedItem embbededContent superclass for the non-multimedia content types. Is this possible?

I was thinking of making a custom model builder for both cases, but this seems a bit like overkill, and it seems like these are pretty normal scenarios which should be covered by standard model mapping so maybe I am missing something.

  • 3
    Have you tried letting the property be of (abstract) type EntityModel? That should allow Entity Models for both Media Items and "normal" Components. You will need proper semantic mappings between your CM Schema and registered View Model types, because the model mapping obviously won't be able to instantiate abstract type EntityModel. – Rick Pannekoek Mar 26 '18 at 14:24
  • That works (with AbstractEntityModel). I added an answer with more info - thanks. – Will Price Apr 6 '18 at 6:54
2

Thanks Rick for the tip. You can indeed make a generic linked item property of type AbstractEntityModel and the DXA will do its best to create the right type of linked entity (multimedia or normal). In order for this to work you need to register models for all possible linked item type (without a view), otherwise you will get errors in the logs and the property value will be null.

Model class property:

private AbstractEntityModel linkedItem;

Module Initializer:

@RegisteredViewModel(modelClass = Image.class), 
@RegisteredViewModel(modelClass = Download.class), 
@RegisteredViewModel(modelClass = DataTable.class)

The only thing about this approach is that it is not really suitable for the first scenario, where I only want additional information for a linked binary and don't care about the linked entity for normal component links. As I have pages containing 20 or 30 links, I end up with a way too heavy page model, if all the (large) linked entities are embedded into it.

To keep to model lighter we will either use a custom model builder, or just have a separate field in our link schema for downloads, and map that to a separate linkedDownload property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.