Copied to clipboard

Flag this post as spam?

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


  • Maarten Mensink 6 posts 78 karma points
    Dec 07, 2021 @ 14:43
    Maarten Mensink
    0

    Access the actual constructed PublishedContent in BlockList

    I am trying to access the model of the current page being rendered in side a blocklist template. The model is being constructed by a RenderController

    I have the following structure.

    • Home
      • Apps
        • App

    When a user navigates to /apps/app1?id=1 The page is rendered using a IContentFinder.

    public class DefaultAppContentFinder : IContentFinder
    {
        private readonly IUmbracoContextAccessor _umbracoContextAccessor;
    
        public DefaultAppContentFinder(IUmbracoContextAccessor umbracoContextAccessor)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }
    
        public bool TryFindContent(IPublishedRequestBuilder request)
        {
            if (!_umbracoContextAccessor.TryGetUmbracoContext(out var context)) return false;
    
            var path = request.Uri.AbsolutePath;
            var query = request.Uri.Query;
            var cache = context.Content;
    
            if (!path.StartsWith("/apps/")) return false;
            if (string.IsNullOrEmpty(query) || !query.Contains("id=")) return false;
    
            var match = cache.GetAtRoot()
                .FirstOrDefault()
                ?.DescendantsOrSelf<App>()
                .FirstOrDefault();
    
            if (match == null) return false;
    
            request.SetPublishedContent(match);
    
            return true;
        }
    }
    

    A for this route a RenderController fetches the additional app info and return a instance of AppDetailViewModel(which inherits from the App Model)

     public class AppController : RenderController
        {
            private readonly IAppService _appService;
            private readonly IUmbracoMapper _mapper;
            private readonly IVariationContextAccessor _variationContextAccessor;
            private readonly ServiceContext _serviceContext;
    
            public AppController(ILogger<RenderController> logger, ICompositeViewEngine compositeViewEngine, IUmbracoContextAccessor umbracoContextAccessor, IAppService appService, IUmbracoMapper mapper, ServiceContext serviceContext, IVariationContextAccessor variationContextAccessor) 
                : base(logger, compositeViewEngine, umbracoContextAccessor)
            {
                _appService = appService;
                _mapper = mapper;
                _serviceContext = serviceContext;
                _variationContextAccessor = variationContextAccessor;
            }
    
            [HttpGet]
            public async Task<IActionResult> Index([FromQuery]Guid? id)
            {
                var app = await _appService.GetAsync(id);
    
                var vm = new AppDetailViewModel(CurrentPage, new PublishedValueFallback(_serviceContext, _variationContextAccessor))
                {
                    App = _mapper.Map<AppViewModel>(app)
                };
    
                return CurrentTemplate(vm);
            }
        }
    

    So far all is good. In the App.cshtml i have the instance of AppDetailViewModel.

    But in the BlockList template when i retrieve the content item from Umbraco.AssignedContentItem i get the instance of the App which was set by the IContentFinder(I assume)

    var page = Umbraco.AssignedContentItem; //instance of App
    

    In the comments there is mention of AssignedContentItem not being the actual item being rendered by the UmbracoContext.

    enter image description here

    How do access the instance of AppDetailViewModel in the BlockList?

    Thanks for any help.

  • andrew shearer 510 posts 659 karma points
    Dec 07, 2021 @ 20:14
    andrew shearer
    0

    hi - Ive been injecting IUmbracoContextAccessor and then using this:

    var contentIpc = this.umbracoContextAccessor.GetRequiredUmbracoContext().PublishedRequest.PublishedContent;
    

    not sure its the official/best way but at the time I was looking into it I couldn't find any documentation. Seems like it needs to be more prominent/convenient to get the current ipc (within a controller or viewcomponent etc)

  • Maarten Mensink 6 posts 78 karma points
    Dec 08, 2021 @ 07:18
    Maarten Mensink
    0

    Hey Andrew,

    Thanks for helping out. I tried your recommendation.

    In the blocklist template i added.

    var page2 = UmbracoContext.PublishedRequest.PublishedContent;
    

    But this isnt the AppDetailViewModel instance that was returned as the Model in AppController.

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft