mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-07-15 14:09:04 +08:00
#16802: Making slug logic extensible / replaceable through events.
--HG-- branch : dev
This commit is contained in:
parent
035f3d3e9c
commit
ca9c5d7b6a
@ -104,6 +104,7 @@
|
||||
<Compile Include="Contents\Shapes.cs" />
|
||||
<Compile Include="Contents\ViewModels\PublishContentViewModel.cs" />
|
||||
<Compile Include="Navigation\Services\MainMenuNavigationProvider.cs" />
|
||||
<Compile Include="Routable\Events\ISlugEventHandler.cs" />
|
||||
<Compile Include="Routable\ResourceManifest.cs" />
|
||||
<Compile Include="Routable\Services\RoutableHomePageProvider.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.Aspects;
|
||||
using Orchard.Core.Common.Models;
|
||||
using Orchard.Core.Routable.Events;
|
||||
using Orchard.Core.Routable.Models;
|
||||
|
||||
namespace Orchard.Core.Routable.Services {
|
||||
public class RoutableService : IRoutableService {
|
||||
private readonly IContentManager _contentManager;
|
||||
private readonly IEnumerable<ISlugEventHandler> _slugEventHandlers;
|
||||
|
||||
public RoutableService(IContentManager contentManager) {
|
||||
public RoutableService(IContentManager contentManager, IEnumerable<ISlugEventHandler> slugEventHandlers) {
|
||||
_contentManager = contentManager;
|
||||
_slugEventHandlers = slugEventHandlers;
|
||||
}
|
||||
|
||||
public void FixContainedPaths(IRoutableAspect part) {
|
||||
@ -47,18 +50,29 @@ namespace Orchard.Core.Routable.Services {
|
||||
if (!string.IsNullOrEmpty(model.Slug) || string.IsNullOrEmpty(model.Title))
|
||||
return;
|
||||
|
||||
var slug = model.Title;
|
||||
var disallowed = new Regex(@"[/:?#\[\]@!$&'()*+,;=\s\""\<\>]+");
|
||||
FillSlugContext slugContext = new FillSlugContext(model.Title);
|
||||
|
||||
slug = disallowed.Replace(slug, "-");
|
||||
slug = slug.Trim('-');
|
||||
foreach(ISlugEventHandler slugEventHandler in _slugEventHandlers) {
|
||||
slugEventHandler.FillingSlugFromTitle(slugContext);
|
||||
}
|
||||
|
||||
if (slug.Length > 1000)
|
||||
slug = slug.Substring(0, 1000);
|
||||
if (!slugContext.Adjusted) {
|
||||
var disallowed = new Regex(@"[/:?#\[\]@!$&'()*+,;=\s\""\<\>]+");
|
||||
|
||||
// dots are not allowed at the begin and the end of routes
|
||||
slug = slug.Trim('.');
|
||||
model.Slug = RemoveDiacritics(slug.ToLower());
|
||||
slugContext.Slug = disallowed.Replace(slugContext.Slug, "-").Trim('-');
|
||||
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user