ZimmertyZim
08/22/2023, 3:10 PMvar englishHomePage = currentContextContent?.GetById(_config.Value.ContentUids.HomePages.English);
string englishHomePageUrl = englishHomePage.Url()
ZimmertyZim
08/22/2023, 3:14 PMMatt Wise
08/22/2023, 4:15 PMZimmertyZim
08/22/2023, 4:16 PMMatt Wise
08/22/2023, 4:47 PMZimmertyZim
08/23/2023, 9:38 AMZimmertyZim
08/25/2023, 1:19 PMpublic IViewComponentResult InvokeAsync()
{
var currentPage = _umbracoContextAccessor
?.GetRequiredUmbracoContext()
?.PublishedRequest
?.PublishedContent;
var homePage = currentPage?.AncestorOrSelf("home");
FooterViewModel footerViewModel = new()
{
FooterLinks = homePage?.Value<IEnumerable<Link>>("footerLinks")
};
return View(footerViewModel);
}
However, like the Url method defined in Umbraco.Extensions, the Umbraco.Extensions Value method can not be found if using outside of a view. If I pull code into the view, it works:
@inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<FooterViewModel>
@{
var currentPage = Umbraco.AssignedContentItem;
var homePage = currentPage.AncestorOrSelf("home");
var footerLinks = homePage?.Value<IEnumerable<Link>>("footerLinks") ?? null!;
// footerLinks is populated here.
}
My first example within a service/helper, this example is on a view component.Ambert
08/25/2023, 1:47 PMAmbert
08/25/2023, 1:49 PMhomePage?.Value<IEnumerable<Link>>("footerLinks")
and just type homepage.FooterLinks
🙂Ambert
08/25/2023, 1:54 PMAmbert
08/25/2023, 1:55 PMZimmertyZim
08/28/2023, 9:22 AMAmbert
08/28/2023, 9:24 AM.Url()
in my controllers if I need the Url from an IpublishedContentAmbert
08/28/2023, 9:25 AM.Url
and .Url()
, no idea 😄Anders Bjerner
08/28/2023, 9:35 AM.Url()
has some optional parameters - eg. for specifying which culture you want the URL for. You can only do that with a method - not with a property. I think that is the reason behind the change.ZimmertyZim
08/29/2023, 11:54 AMIEnumerable<IPublishedContent> navigationPages = homePage.Children(x => x.IsVisible() && x.DocumentTypeAlias != "home" && !x.GetPropertyValue<bool>("hideFromNavigation"));
the new code being this, this is defined in a ViewComponent:
//_variationContextAccessor and _publishedValueFallback are injected
var naviagtionPages = homePage
?.Children(_variationContextAccessor, x => x.IsVisible(_publishedValueFallback) && !x.HasValue("hideFromTopNavigation"));
As far as I understand, _publiushedValueFallback
and _variationContentAccesor
shouldn't be needed due to Umbraco.Extensions? I am aware that I should be using the Models Builder, just not sure how to do it in his scenario when looking at multiple content types.ZimmertyZim
08/29/2023, 11:56 AMAnders Bjerner
08/29/2023, 12:01 PM_variationContextAccessor
or a _publishedValueFallback
.
I think you find the friendly extension methods by importing the Umbraco.Extensions
namespace.ZimmertyZim
08/29/2023, 12:03 PMZimmertyZim
08/29/2023, 12:07 PMusing Microsoft.AspNetCore.Mvc;
using MyApplication.Models;
using MyApplication.Web.Models.ViewModels;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.Web;
using Umbraco.Extensions;
namespace MyApplication.Web.ViewComponents.SiteLayout;
[ViewComponent(Name = "TopNavigation")]
public class TopNavigationViewComponent : ViewComponent
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly IVariationContextAccessor _variationContextAccessor;
private readonly IPublishedValueFallback _publishedValueFallback;
public TopNavigationViewComponent(
IUmbracoContextAccessor umbracoContextAccessor,
IVariationContextAccessor variationContextAccessor,
IPublishedValueFallback publishedValueFallback
)
{
_umbracoContextAccessor = umbracoContextAccessor;
_variationContextAccessor = variationContextAccessor;
_publishedValueFallback = publishedValueFallback;
}
public IViewComponentResult Invoke()
{
var currentPage = _umbracoContextAccessor
?.GetRequiredUmbracoContext()
?.PublishedRequest
?.PublishedContent;
var homePage = Helpers.Site.GetHomeNode(currentPage ?? null!) as Home;
var naviagtionPages = homePage
?.Children(_variationContextAccessor, x => x.IsVisible(_publishedValueFallback) && !x.HasValue("hideFromTopNavigation") && x.GetType() != typeof(Home));
TopNavigationViewModel topNavigationViewModel = new()
{
NavigationPages = naviagtionPages
};
return View("/Views/Shared/Components/SiteLayout/TopNavigation.cshtml", topNavigationViewModel);
}
}
ZimmertyZim
08/29/2023, 12:07 PMZimmertyZim
08/29/2023, 12:08 PMA hub and casual space for you to interact with fellow community members and learn more about Umbraco!
Powered by