Creating Patterns and Default Patterns for each culture installed when attaching an AutoroutePart to a Content Type.

Related to issue #5195
This commit is contained in:
Skrypt 2015-06-07 21:12:11 -04:00
parent 60ce2d777c
commit 23f3f29a47
2 changed files with 109 additions and 0 deletions

View File

@ -95,6 +95,10 @@
<Project>{475B6C45-B27C-438B-8966-908B9D6D1077}</Project>
<Name>Orchard.Alias</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.ContentTypes\Orchard.ContentTypes.csproj">
<Project>{0e7646e8-fe8f-43c1-8799-d97860925ec4}</Project>
<Name>Orchard.ContentTypes</Name>
</ProjectReference>
<ProjectReference Include="..\Orchard.Tokens\Orchard.Tokens.csproj">
<Project>{6F759635-13D7-4E94-BCC9-80445D63F117}</Project>
<Name>Orchard.Tokens</Name>
@ -102,6 +106,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Commands\AutorouteCommands.cs" />
<Compile Include="Providers\ContentDefinition\ContentDefinitionEventHandler.cs" />
<Compile Include="ResourceManifest.cs" />
<Compile Include="Services\AliasResolverSelector.cs" />
<Compile Include="Services\PathResolutionService.cs" />

View File

@ -0,0 +1,104 @@
using Orchard.Autoroute.Models;
using Orchard.Autoroute.Services;
using Orchard.Autoroute.Settings;
using Orchard.ContentManagement;
using Orchard.ContentManagement.MetaData;
using Orchard.ContentTypes.Events;
using Orchard.Localization.Services;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Orchard.Autoroute.Providers.ContentDefinition {
public class ContentDefinitionEventHandler : IContentDefinitionEventHandler {
private readonly ICultureManager _cultureManager;
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly IOrchardServices _orchardServices;
private readonly Lazy<IAutorouteService> _autorouteService;
private readonly IContentManager _contentManager;
public ContentDefinitionEventHandler(
IContentManager contentManager,
Lazy<IAutorouteService> autorouteService,
IOrchardServices orchardServices,
IContentDefinitionManager contentDefinitionManager,
ICultureManager cultureManager) {
_cultureManager = cultureManager;
_contentDefinitionManager = contentDefinitionManager;
_orchardServices = orchardServices;
_autorouteService = autorouteService;
_contentManager = contentManager;
}
public void ContentTypeCreated(ContentTypeCreatedContext context) {
}
public void ContentTypeRemoved(ContentTypeRemovedContext context) {
}
public void ContentTypeImporting(ContentTypeImportingContext context) {
}
public void ContentTypeImported(ContentTypeImportedContext context) {
}
public void ContentPartCreated(ContentPartCreatedContext context) {
}
public void ContentPartRemoved(ContentPartRemovedContext context) {
}
public void ContentPartAttached(ContentPartAttachedContext context) {
if (context.ContentPartName == "AutoroutePart") {
//Create pattern and default pattern for each culture installed
//get cultures
var SiteCultures = _cultureManager.ListCultures().ToList();
//Create Patterns and DefaultPatterns
var settings = new AutorouteSettings {
Patterns = new List<RoutePattern>()
};
List<RoutePattern> newPatterns = new List<RoutePattern>();
List<DefaultPattern> newDefaultPatterns = new List<DefaultPattern>();
foreach (string culture in SiteCultures) {
newPatterns.Add(new RoutePattern {
Name = "Title",
Description = "my-title",
Pattern = "{Content.Slug}",
Culture = culture
});
newDefaultPatterns.Add(new DefaultPattern {
Culture = culture,
PatternIndex = "0"
});
}
settings.Patterns = newPatterns;
settings.DefaultPatterns = newDefaultPatterns;
//Update Settings
_contentDefinitionManager.AlterTypeDefinition(context.ContentTypeName, builder => builder.WithPart("AutoroutePart", settings.Build));
//TODO Generate URL's for existing content items
}
}
public void ContentPartDetached(ContentPartDetachedContext context) {
}
public void ContentPartImporting(ContentPartImportingContext context) {
}
public void ContentPartImported(ContentPartImportedContext context) {
}
public void ContentFieldAttached(ContentFieldAttachedContext context) {
}
public void ContentFieldDetached(ContentFieldDetachedContext context) {
}
}
}