Last active
September 4, 2020 08:42
-
-
Save TimGeyssens/5b4b1ba925d28af2729e171319c22636 to your computer and use it in GitHub Desktop.
Extending the UmbracoHelper in Umbraco v8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Web.PublishedModels; | |
namespace MyWebSite.Custom | |
{ | |
public interface ISiteService | |
{ | |
Website GetWebsite(int id); | |
SearchPage GetSearchPage(int id); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<add key="Umbraco.ModelsBuilder.Enable" value="true" /> | |
<add key="Umbraco.ModelsBuilder.ModelsMode" value="LiveAppData" /> | |
<add key="Umbraco.ModelsBuilder.ModelsDirectory" value="~/Models.Generated" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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