Try our conversational search powered by Generative AI!

Per Magne Skuseth
Sep 23, 2015
  7015
(6 votes)

Thumbnails for blocks!

Can blocks have thumbnails in the assets pane in the same way as images do? I got this question from someone who needed just this. They had lots and lots of blocks and wanted to ease the navigation for their editors, and most of their blocks already had some sort of image property.
The answer? Yes! And here’s how you could do it:

Start by creating a simple interface that contains a property of type ContentRefrence. This will be used as a pointer to the image that will be used as a thumbnail.

public interface IHaveThumbnail
{
    ContentReference ThumbnailReference { get; }
}

 

Implement the interface on your block type. It should look something like this:

public class TeaserBlock : SiteBlockData , IHaveThumbnail
{
    public virtual string Heading { get; set; }
 
    [UIHint(UIHint.Image)]
    public virtual ContentReference Image { get; set; }
 
    public virtual ContentReference ThumbnailReference
    {
        get { return Image; }
    }
}

 

In order use the new thumbnail property, the model that is being created for the blocks component in the assets pane needs to be modified to include the new thumbnail property. This can be done by creating a custom implmentation of IModelTransform. TransformInstance will create the actual model - notice that base.TransformInstance is invoked first. If the current target implements IHaveThumbnail, target.ThumbnailUrl will be populated.

[ServiceConfiguration(typeof(IModelTransform), Lifecycle = ServiceInstanceScope.Singleton)]
public class ThumbnailModelTransform : StructureStoreModelTransform
{
    private readonly IContentLoader _contentLoader;
    public ThumbnailModelTransform(IContentRepository contentRepository, IContentQueryHelper contentQueryHelper,
        IContentProviderManager contentProviderManager, ILanguageBranchRepository languageBranchRepository,
        IContentLanguageSettingsHandler contentLanguageSettingsHandler, SiteDefinitionRepository siteDefinitionRepository,
        IContentLoader contentLoader) :
        base(contentRepository, contentQueryHelper, contentProviderManager, languageBranchRepository, contentLanguageSettingsHandler, siteDefinitionRepository)
    {
        _contentLoader = contentLoader;
    }
 
    public override void TransformInstance(IContent source, StructureStoreContentDataModel target, IModelTransformContext context)
    {
        base.TransformInstance(source, target, context);
        var contentWithThumbnail = source as IHaveThumbnail;
        if (contentWithThumbnail != null && contentWithThumbnail.ThumbnailReference != null 
            && contentWithThumbnail.ThumbnailReference != ContentReference.EmptyReference)
        {
            IContent imageContent;
            if (_contentLoader.TryGet(contentWithThumbnail.ThumbnailReference, out imageContent))
            {
                var urlBuilder = new UrlBuilder(imageContent.PreviewUrl());
                target.ThumbnailUrl = string.Format("{0}/{1}", urlBuilder.Path, "thumbnail");
            }
        }
    }
}

 

Make sure that ThumbnailModelTransform is being put to use by swapping out the default IModelTransform.

container.For<IModelTransform>().Use<ThumbnailModelTransform>();

 

With the ThumbnailModelTransform in place, thumbnail images will now be rendered in the block component in assets pane. However, a little bit of styling is needed to make it look nice and tidy.

.epi-blockList .dgrid-row {
        height: 36px;
}
 
.epi-blockList .dgrid-row img {
        margin-right: 2px;
        height: 36px;
}
.epi-blockList .dgrid-row span, .epi-blockList .dgrid-row div {
        margin-top: 10px;
}
.epi-blockList .dgrid-row .epi-iconObjectSharedBlock {
        margin-right: 8px;
        margin-left: 8px;
}

The css file is being referenced in the clientResources section of module.config:
<add name="epi-cms.widgets.base" path="Styles/Styles.css" resourceType="Style"/>

 

Final result - notice how the teaser blocks now have thumbnails:

 dat block thumbnailz

Sep 23, 2015

Comments

Sep 23, 2015 10:30 PM

Nice!

Arild Henrichsen
Arild Henrichsen Sep 24, 2015 07:11 AM

Great usability improvement!

Mads Storm Hansen
Mads Storm Hansen Sep 26, 2015 10:24 AM

Great improvement.

Should I add a new "Module", to get the CSS injected in Edit mode ?

Per Magne Skuseth
Per Magne Skuseth Sep 28, 2015 08:13 AM

@Mads, you should already have a module.config in your project. Add the css reference under the section. Check out how it's done in Alloy if you are not sure :-)

Please login to comment.
Latest blogs
Solving the mystery of high memory usage

Sometimes, my work is easy, the problem could be resolved with one look (when I’m lucky enough to look at where it needs to be looked, just like th...

Quan Mai | Apr 22, 2024 | Syndicated blog

Search & Navigation reporting improvements

From version 16.1.0 there are some updates on the statistics pages: Add pagination to search phrase list Allows choosing a custom date range to get...

Phong | Apr 22, 2024

Optimizely and the never-ending story of the missing globe!

I've worked with Optimizely CMS for 14 years, and there are two things I'm obsessed with: Link validation and the globe that keeps disappearing on...

Tomas Hensrud Gulla | Apr 18, 2024 | Syndicated blog

Visitor Groups Usage Report For Optimizely CMS 12

This add-on offers detailed information on how visitor groups are used and how effective they are within Optimizely CMS. Editors can monitor and...

Adnan Zameer | Apr 18, 2024 | Syndicated blog