mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-01-19 17:51:45 +08:00
#16802: Making slug logic extensible / replaceable through events.
--HG-- branch : dev
This commit is contained in:
@@ -104,6 +104,7 @@
|
|||||||
<Compile Include="Contents\Shapes.cs" />
|
<Compile Include="Contents\Shapes.cs" />
|
||||||
<Compile Include="Contents\ViewModels\PublishContentViewModel.cs" />
|
<Compile Include="Contents\ViewModels\PublishContentViewModel.cs" />
|
||||||
<Compile Include="Navigation\Services\MainMenuNavigationProvider.cs" />
|
<Compile Include="Navigation\Services\MainMenuNavigationProvider.cs" />
|
||||||
|
<Compile Include="Routable\Events\ISlugEventHandler.cs" />
|
||||||
<Compile Include="Routable\ResourceManifest.cs" />
|
<Compile Include="Routable\ResourceManifest.cs" />
|
||||||
<Compile Include="Routable\Services\RoutableHomePageProvider.cs" />
|
<Compile Include="Routable\Services\RoutableHomePageProvider.cs" />
|
||||||
<Compile Include="Contents\ViewModels\ListContentsViewModel.cs" />
|
<Compile Include="Contents\ViewModels\ListContentsViewModel.cs" />
|
||||||
|
|||||||
17
src/Orchard.Web/Core/Routable/Events/ISlugEventHandler.cs
Normal file
17
src/Orchard.Web/Core/Routable/Events/ISlugEventHandler.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
using Orchard.Events;
|
||||||
|
|
||||||
|
namespace Orchard.Core.Routable.Events {
|
||||||
|
public interface ISlugEventHandler : IEventHandler {
|
||||||
|
void FillingSlugFromTitle(FillSlugContext context);
|
||||||
|
void FilledSlugFromTitle(FillSlugContext context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public class FillSlugContext {
|
||||||
|
public FillSlugContext(string slug) {
|
||||||
|
Slug = slug;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string Slug { get; set; }
|
||||||
|
public bool Adjusted { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,14 +7,17 @@ using System.Text.RegularExpressions;
|
|||||||
using Orchard.ContentManagement;
|
using Orchard.ContentManagement;
|
||||||
using Orchard.ContentManagement.Aspects;
|
using Orchard.ContentManagement.Aspects;
|
||||||
using Orchard.Core.Common.Models;
|
using Orchard.Core.Common.Models;
|
||||||
|
using Orchard.Core.Routable.Events;
|
||||||
using Orchard.Core.Routable.Models;
|
using Orchard.Core.Routable.Models;
|
||||||
|
|
||||||
namespace Orchard.Core.Routable.Services {
|
namespace Orchard.Core.Routable.Services {
|
||||||
public class RoutableService : IRoutableService {
|
public class RoutableService : IRoutableService {
|
||||||
private readonly IContentManager _contentManager;
|
private readonly IContentManager _contentManager;
|
||||||
|
private readonly IEnumerable<ISlugEventHandler> _slugEventHandlers;
|
||||||
|
|
||||||
public RoutableService(IContentManager contentManager) {
|
public RoutableService(IContentManager contentManager, IEnumerable<ISlugEventHandler> slugEventHandlers) {
|
||||||
_contentManager = contentManager;
|
_contentManager = contentManager;
|
||||||
|
_slugEventHandlers = slugEventHandlers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void FixContainedPaths(IRoutableAspect part) {
|
public void FixContainedPaths(IRoutableAspect part) {
|
||||||
@@ -47,18 +50,29 @@ namespace Orchard.Core.Routable.Services {
|
|||||||
if (!string.IsNullOrEmpty(model.Slug) || string.IsNullOrEmpty(model.Title))
|
if (!string.IsNullOrEmpty(model.Slug) || string.IsNullOrEmpty(model.Title))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var slug = model.Title;
|
FillSlugContext slugContext = new FillSlugContext(model.Title);
|
||||||
var disallowed = new Regex(@"[/:?#\[\]@!$&'()*+,;=\s\""\<\>]+");
|
|
||||||
|
|
||||||
slug = disallowed.Replace(slug, "-");
|
foreach(ISlugEventHandler slugEventHandler in _slugEventHandlers) {
|
||||||
slug = slug.Trim('-');
|
slugEventHandler.FillingSlugFromTitle(slugContext);
|
||||||
|
}
|
||||||
|
|
||||||
if (slug.Length > 1000)
|
if (!slugContext.Adjusted) {
|
||||||
slug = slug.Substring(0, 1000);
|
var disallowed = new Regex(@"[/:?#\[\]@!$&'()*+,;=\s\""\<\>]+");
|
||||||
|
|
||||||
// dots are not allowed at the begin and the end of routes
|
slugContext.Slug = disallowed.Replace(slugContext.Slug, "-").Trim('-');
|
||||||
slug = slug.Trim('.');
|
|
||||||
model.Slug = RemoveDiacritics(slug.ToLower());
|
if (slugContext.Slug.Length > 1000)
|
||||||
|
slugContext.Slug = slugContext.Slug.Substring(0, 1000);
|
||||||
|
|
||||||
|
// dots are not allowed at the begin and the end of routes
|
||||||
|
slugContext.Slug = RemoveDiacritics(slugContext.Slug.Trim('.').ToLower());
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (ISlugEventHandler slugEventHandler in _slugEventHandlers) {
|
||||||
|
slugEventHandler.FilledSlugFromTitle(slugContext);
|
||||||
|
}
|
||||||
|
|
||||||
|
model.Slug = slugContext.Slug;
|
||||||
}
|
}
|
||||||
|
|
||||||
public string GenerateUniqueSlug(IRoutableAspect part, IEnumerable<string> existingPaths) {
|
public string GenerateUniqueSlug(IRoutableAspect part, IEnumerable<string> existingPaths) {
|
||||||
|
|||||||
Reference in New Issue
Block a user