Skip to content

Instantly share code, notes, and snippets.

@TimGeyssens
Last active September 4, 2020 08:42
Show Gist options
  • Save TimGeyssens/5b4b1ba925d28af2729e171319c22636 to your computer and use it in GitHub Desktop.
Save TimGeyssens/5b4b1ba925d28af2729e171319c22636 to your computer and use it in GitHub Desktop.
Extending the UmbracoHelper in Umbraco v8
using Umbraco.Web.PublishedModels;
namespace MyWebSite.Custom
{
public interface ISiteService
{
Website GetWebsite(int id);
SearchPage GetSearchPage(int id);
}
}
<add key="Umbraco.ModelsBuilder.Enable" value="true" />
<add key="Umbraco.ModelsBuilder.ModelsMode" value="LiveAppData" />
<add key="Umbraco.ModelsBuilder.ModelsDirectory" value="~/Models.Generated" />
using Umbraco.Core.Composing;
using Umbraco.Core;
namespace MyWebSite.Custom
{
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class RegisterSiteServiceComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register<ISiteService, SiteService>();
}
}
}
using System.Linq;
using Umbraco.Core.Models.PublishedContent;
using Umbraco.Web;
using Umbraco.Web.PublishedModels;
namespace MyWebSite.Custom
{
public class SiteService : ISiteService
{
private readonly IUmbracoContextFactory _umbracoContextFactory;
public SiteService(IUmbracoContextFactory umbracoContextFactory)
{
_umbracoContextFactory = umbracoContextFactory;
}
public Website GetWebsite(int id)
{
Website website = null;
using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
{
website = umbracoContextReference.UmbracoContext.Content.GetById(id).AncestorOrSelf(Website.ModelTypeAlias) as Website;
}
return website;
}
public SearchPage GetSearchPage(int id)
{
SearchPage searchpage = null;
using (UmbracoContextReference umbracoContextReference = _umbracoContextFactory.EnsureUmbracoContext())
{
searchpage = GetWebsite(id).ChildrenOfType(SearchPage.ModelTypeAlias) as SearchPage;
}
return alias;
}
}
}
using LightInject;
using Umbraco.Core;
using Umbraco.Web;
using Umbraco.Web.PublishedModels;
namespace MyWebSite.Custom
{
public static class UmbracoHelperExtensions
{
public static Website GetWebsite(this UmbracoHelper umbracoHelper)
{
var siteService = Umbraco.Web.Composing.Current.Factory.GetInstance<ISiteService>();
return siteService.GetWebsite(umbracoHelper.AssignedContentItem.Id);
}
public static SearchPage GetSearchPage(this UmbracoHelper umbracoHelper)
{
var siteService = Umbraco.Web.Composing.Current.Factory.GetInstance<ISiteService>();
return siteService.GetSearchPage(umbracoHelper.AssignedContentItem.Id);
}
}
}
@if (Umbraco.GetWebsite().MainNavigation.Any())
{
<ul class="navbar-nav ml-auto">
@foreach (var link in Umbraco.GetWebsite().MainNavigation)
{
<li class="nav-item">
<a class="nav-link" href="@link.Url" target="@link.Target">@link.Name</a>
</li>
}
</ul>
}
@if (Umbraco.GetSearchPage() != null)
{
<form method="post" action="@Umbraco.GetSearchPage().Url">
<input name="q" type="text" />
<input type="submit" value="Submit">
</form>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment