mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2026-02-09 09:16:41 +08:00
You can't create two Template content items with the same name (title) any more, fixes #6360
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.Drivers;
|
||||
using Orchard.Core.Title.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Templates.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace Orchard.Templates.Drivers {
|
||||
public class TitlePartDriver : ContentPartDriver<TitlePart> {
|
||||
private readonly IContentManager _contentManager;
|
||||
|
||||
public Localizer T { get; set; }
|
||||
|
||||
public TitlePartDriver(IContentManager contentManager) {
|
||||
_contentManager = contentManager;
|
||||
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
protected override DriverResult Editor(TitlePart part, IUpdateModel updater, dynamic shapeHelper) {
|
||||
if (!part.ContentItem.Has<ShapePart>()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
updater.TryUpdateModel(part, Prefix, null, null);
|
||||
|
||||
// We need to query for the content type names because querying for content parts has no effect on the database side.
|
||||
var contentTypesWithShapePart = _contentManager
|
||||
.GetContentTypeDefinitions()
|
||||
.Where(typeDefinition => typeDefinition.Parts.Any(partDefinition => partDefinition.PartDefinition.Name == "ShapePart"))
|
||||
.Select(typeDefinition => typeDefinition.Name)
|
||||
.ToArray();
|
||||
|
||||
var existingShapeCount = _contentManager
|
||||
.Query(VersionOptions.Latest, contentTypesWithShapePart)
|
||||
.Where<TitlePartRecord>(record => record.Title == part.Title && record.ContentItemRecord.Id != part.ContentItem.Id)
|
||||
.Count();
|
||||
|
||||
if (existingShapeCount > 0) {
|
||||
updater.AddModelError("ShapeNameAlreadyExists", T("A template with the given name already exists."));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -162,6 +162,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
<Compile Include="Controllers\AdminController.cs" />
|
||||
<Compile Include="Drivers\TitlePartDriver.cs" />
|
||||
<Compile Include="Helpers\StringExtensions.cs" />
|
||||
<Compile Include="Handlers\ShapePartHandler.cs" />
|
||||
<Compile Include="Drivers\ShapePartDriver.cs" />
|
||||
|
||||
Reference in New Issue
Block a user