Copied to clipboard

Flag this post as spam?

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


  • Prasant 10 posts 90 karma points
    Dec 18, 2022 @ 05:28
    Prasant
    0

    How to render multiMediaPicker property in macros in Umbraco?

    I have used this to render my images under macros

    var images = Model.MacroParameters["images"] as
    IEnumerable<IPublishedContent>;
    
    
    <div id="blog" class="grid-layout post-3-columns m-b-30" data-item="post-item">
                                @if(images != null)
                                {
                                    @foreach(var item in images)
                                    {
                                      <div class="post-item border">
                                        <div class="post-item-wrap">
                                          <div class="post-image">
                                            <a href="#">
                                              <img alt="" src="@item.Url()">
                                            </a>
                                          </div>
                                        </div>
                                      </div>
                                    }
                                }
    
                            </div>
    

    But It does fail to show any images. Please help me through this.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Dec 18, 2022 @ 06:23
    Huw Reddick
    0

    Hi Prasant,

    There is a snippet provided that you can use to do this. If you look at the code, the media items are passed as a comma delimited string of Id's not IEnumerable<IPublishedContent>

    @inherits Umbraco.Cms.Web.Common.Macros.PartialViewMacroPage
    @using Umbraco.Cms.Core
    @using Umbraco.Cms.Core.Media
    @using Umbraco.Cms.Core.Models.PublishedContent
    @using Umbraco.Cms.Core.Routing
    @using Umbraco.Extensions
    
    @inject IPublishedValueFallback PublishedValueFallback
    @inject IPublishedContentQuery PublishedContentQuery
    @inject IVariationContextAccessor VariationContextAccessor
    @inject IPublishedUrlProvider PublishedUrlProvider
    @inject IImageUrlGenerator ImageUrlGenerator
    
    @*
        Macro to display a gallery of images from the Media section.
        Works with either a 'Single Media Picker' or a 'Multiple Media Picker' macro parameter (see below).
    
        How it works:
            - Confirm the macro parameter has been passed in with a value
            - Loop through all the media Ids passed in (might be a single item, might be many)
            - Display any individual images, as well as any folders of images
    
        Macro Parameters To Create, for this macro to work:
        Alias:mediaIds     Name:Select folders and/or images    
        Type: Multiple Media Picker
        Type: (note: You can use a Single Media Picker if that's more appropriate to your needs)
    *@
    
    @{ var mediaIds = Model?.MacroParameters["mediaIds"] as string; }
    
    @if (mediaIds != null)
    {
        <div class="row">
            @foreach (var mediaId in mediaIds.Split(','))
            {
                var media = PublishedContentQuery.Media(mediaId);
    
                @* a single image *@
                if (media.IsDocumentType("Image"))
                {
                    <div class="col-xs-6 col-md-3">
                        <a href="@media.Url(PublishedUrlProvider)" class="thumbnail">
                            <img src="@media.GetCropUrl(ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, width: 200, height: 200)" alt="@media.Name"/>
                        </a>
                    </div>
                }
    
                @* a folder with images under it *@
                foreach (var image in media.Children(VariationContextAccessor))
                {
                    <div class="col-xs-6 col-md-3">
                        <a href="@image.Url(PublishedUrlProvider)" class="thumbnail">
                            <img src="@image.GetCropUrl(ImageUrlGenerator, PublishedValueFallback, PublishedUrlProvider, width: 200, height: 200)" alt="@image.Name"/>
                        </a>
                    </div>
                }
            }
        </div>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft