mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-07-16 07:04:52 +08:00
Adds field removal to ContentTypeStep (#8282)
This commit is contained in:
parent
a0a1d1037e
commit
70c4541977
@ -126,7 +126,7 @@
|
||||
<Compile Include="Providers\Executors\RemoveContentDefinitionStep.cs" />
|
||||
<Compile Include="Providers\Executors\RemoveContentStep.cs" />
|
||||
<Compile Include="Providers\Executors\RecipesStep.cs" />
|
||||
<Compile Include="Providers\Executors\RemovePartFromContentTypeStep.cs" />
|
||||
<Compile Include="Providers\Executors\RemoveFromContentTypeStep.cs" />
|
||||
<Compile Include="ViewModels\ContentExecutionStepViewModel.cs" />
|
||||
<Compile Include="Providers\Executors\ContentStep.cs" />
|
||||
<Compile Include="Providers\Executors\ContentDefinitionStep.cs" />
|
||||
|
@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.Xml;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentTypes.Events;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
|
||||
namespace Orchard.Recipes.Providers.Executors {
|
||||
public class RemoveFromContentTypeStep : RecipeExecutionStep {
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly IContentDefinitionEventHandler _contentDefinitionEventHandlers;
|
||||
|
||||
public RemoveFromContentTypeStep(RecipeExecutionLogger logger, IContentDefinitionManager contentDefinitionManager,
|
||||
IContentDefinitionEventHandler contentDefinitonEventHandlers) : base(logger) {
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_contentDefinitionEventHandlers = contentDefinitonEventHandlers;
|
||||
}
|
||||
|
||||
public override string Name {
|
||||
get { return "RemoveFromContentType"; }
|
||||
}
|
||||
|
||||
public override LocalizedString DisplayName {
|
||||
get { return T("Remove From Content Type"); }
|
||||
}
|
||||
|
||||
public override LocalizedString Description {
|
||||
get { return T("Removes a list of parts and fields from a content type."); }
|
||||
}
|
||||
|
||||
// <RemoveFromContentType>
|
||||
// <Blog>
|
||||
// <Parts>
|
||||
// </Parts>
|
||||
// <Fields>
|
||||
// </Fields>
|
||||
// </Blog>
|
||||
// </RemoveFromContentType>
|
||||
public override void Execute(RecipeExecutionContext context) {
|
||||
foreach (var metadataElementType in context.RecipeStep.Step.Elements()) {
|
||||
Logger.Debug("Processing element '{0}'.", metadataElementType.Name.LocalName);
|
||||
var typeName = XmlConvert.DecodeName(metadataElementType.Name.LocalName);
|
||||
|
||||
foreach (var metadataElement in metadataElementType.Elements()) {
|
||||
switch (metadataElement.Name.LocalName) {
|
||||
case "Parts":
|
||||
foreach (var element in metadataElement.Elements()) {
|
||||
var partName = XmlConvert.DecodeName(element.Name.LocalName);
|
||||
|
||||
Logger.Information("Removing content part '{0}' from content type '{1}'.", partName, typeName);
|
||||
try {
|
||||
_contentDefinitionManager.AlterTypeDefinition(typeName, typeBuilder => typeBuilder.RemovePart(partName));
|
||||
_contentDefinitionEventHandlers.ContentPartDetached(new ContentPartDetachedContext { ContentTypeName = typeName, ContentPartName = partName });
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "Error while removing content part '{0}' from content type'{1}'.", partName, typeName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case "Fields":
|
||||
foreach (var element in metadataElement.Elements()) {
|
||||
var fieldName = XmlConvert.DecodeName(element.Name.LocalName);
|
||||
|
||||
Logger.Information("Removing content field '{0}' from content type '{1}'.", fieldName, typeName);
|
||||
try {
|
||||
_contentDefinitionManager.AlterPartDefinition(typeName, typeBuilder => typeBuilder.RemoveField(fieldName));
|
||||
_contentDefinitionEventHandlers.ContentFieldDetached(new ContentFieldDetachedContext { ContentPartName = typeName, ContentFieldName = fieldName });
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "Error while removing content field '{0}' from content type'{1}'.", fieldName, typeName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
Logger.Warning("Unrecognized element '{0}' encountered; skipping",
|
||||
metadataElement.Name.LocalName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
using System;
|
||||
using System.Xml;
|
||||
using Orchard.ContentTypes.Services;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
|
||||
namespace Orchard.Recipes.Providers.Executors {
|
||||
public class RemovePartFromContentTypeStep : RecipeExecutionStep {
|
||||
private readonly IContentDefinitionService _contentDefinitionService;
|
||||
|
||||
public RemovePartFromContentTypeStep(RecipeExecutionLogger logger, IContentDefinitionService contentDefinitionService) : base(logger) {
|
||||
_contentDefinitionService = contentDefinitionService;
|
||||
}
|
||||
|
||||
public override string Name {
|
||||
get { return "RemovePartFromContentType"; }
|
||||
}
|
||||
|
||||
public override LocalizedString DisplayName {
|
||||
get { return T("Remove Part From Content Type"); }
|
||||
}
|
||||
|
||||
public override LocalizedString Description {
|
||||
get { return T("Removes a list of parts from a content type."); }
|
||||
}
|
||||
|
||||
// <RemovePartFromContentType>
|
||||
// <Blog>
|
||||
// <Parts>
|
||||
// </Parts>
|
||||
// </Blog>
|
||||
// </RemovePartFromContentType>
|
||||
public override void Execute(RecipeExecutionContext context) {
|
||||
foreach (var metadataElementType in context.RecipeStep.Step.Elements()) {
|
||||
Logger.Debug("Processing element '{0}'.", metadataElementType.Name.LocalName);
|
||||
var typeName = XmlConvert.DecodeName(metadataElementType.Name.LocalName);
|
||||
|
||||
foreach (var metadataElement in metadataElementType.Elements()) {
|
||||
switch (metadataElement.Name.LocalName) {
|
||||
case "Parts":
|
||||
foreach (var element in metadataElement.Elements()) {
|
||||
var partName = XmlConvert.DecodeName(element.Name.LocalName);
|
||||
|
||||
Logger.Information("Removing content part '{0}' from content type '{1}'.", typeName, partName);
|
||||
try {
|
||||
_contentDefinitionService.RemovePartFromType(partName, typeName);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
Logger.Error(ex, "Error while removing content part '{0}' from content type'{1}'.", typeName, partName);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
Logger.Warning("Unrecognized element '{0}' encountered; skipping",
|
||||
metadataElement.Name.LocalName);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user